0
將.dat文件導入R控制檯。希望獲得一張乾淨的表格,以便我可以將其轉換回CSV並使用Excel中的其他CSV文件進行操作。R數據清理失敗:.dat清理數據幀以清理CSV文件
下面是整個R控制檯會話。該表看起來在控制檯中對齊,但當導出爲CSV時肯定不會。嘗試一下代碼,看看會發生什麼。
sun <- readLines("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat")
head(sun)
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE, sep=',')
Warning messages:
1: In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
EOF within quoted string
2: In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
number of items read is not a multiple of the number of columns
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE, sep=' ')
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 87 elements
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE)
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 9 did not have 15 elements
sun <- read.csv("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE)
sundf <- data.frame(sun)
write.csv(sundf, "sun.csv")
第二個嘗試:
sun <- read_fwf("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", col_positions = fwf_positions())
Error in stopifnot(length(start) == length(end)) :
argument "start" is missing, with no default
數據不被界定爲一個可用的途徑;它確實是一個固定寬度的格式。使用'read.fwf'而不是'read.table'。有關更多信息,請參閱文檔(或者使用'readr'包中的'read_fwf',這樣更有效)。 – MrFlick