Problems loading .csv file into RStudio. EOF within quoted string
当我将此文件Chicago_Crimes_2005_to_2007.csv(链接https://www.kaggle.com/currie32/crimes-in-chicago)加载到RStudio中时,总是收到错误消息(Warnmeldung:
在scan(file = file,what = what,sep = sep,quote = quote,dec = dec ,: Zeichenkette中的EOF /英语:带有引号的字符串中的EOF)中,并不包括所有的内容。 你知道如何解决这个问题吗? 与其他3个文件,我没有问题。 我正在使用此代码:
1 | c2 = read.csv("Chicago_Crimes_2005_to_2007.csv", header = TRUE) |
我试图用以下代码修复它:
1 | c2 = read.csv("Chicago_Crimes_2005_to_2007.csv", header = TRUE, quote ="", row.names = NULL, stringsAsFactors = FALSE). |
没有锻炼。 我在这里用相同的错误在stackoverflow中尝试了所有答案。 没有任何帮助。 自1周以来没有成功。 希望有人能帮助我。 在RStudio中使用R。
这是一个读取脚本的版本,该脚本解析文件第一行中的列名,并使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | library(readr) library(tidyr) # read first row and clean column names colNamesData <- read_csv("./data/Chicago_Crimes_2005_to_2007.csv",col_names=FALSE,n_max=1) # set NA to Row Number colNamesData[1,1] <-"Row Number" # use tidyr::gather() to turn rows into columns xColNames <- gather(colNamesData) # use gsub() to replace blanks with periods so data can be used as column names xColNames$value <- gsub("",".",xColNames$value) # read with readr::read_csv() and set column names to data extracted from first row # skip first row because it contains bad column names and is missing the first column name crimeData <- read_csv("./data/Chicago_Crimes_2005_to_2007.csv",col_names=xColNames$value,skip=1) # last row in file is row number 6254267 summary(crimeData$Row.Number) |
...以及输出:
1 2 3 4 | > summary(crimeData$Row.Number) Min. 1st Qu. Median Mean 3rd Qu. Max. 0 235792 471370 1944429 5601310 6254267 > |
注意:该文件不能正确读取所有记录,因为在行533,719中,记录似乎以变量名的冗余列表结尾。
要解决此问题,必须手动编辑数据以删除变量名称的冗余列表,或者在错误周围进行编码。
有趣的是,原始数据文件的行533,720中的行号计数从0重新开始,这表明创建此数据的人员错误地将多个文件连接起来以创建此数据文件。
干得好:
1 2 | require(tidyverse) df <- readr::read_csv("Chicago_Crimes_2005_to_2007.csv") |
您可能会决定清除列名称,因为某些列名称中有空格,如果这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | colnames(df) <- c("rowNo", "ID", "Case.Number", "Date", "Block", "IUCR", "Primary.Type", "Description", "Location.Description", "Arrest", "Domestic", "Beat", "District", "Ward", "Community.Area", "FBI.Code", "X.Coordinate", "Y.Coordinate", "Year", "Updated.On", "Latitude", "Longitude", "Location") |