0

.dat ファイルを R コンソールにインポートしています。きれいなテーブルを取得して、それを CSV に変換し、Excel で他の 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")

2 回目の試行:

 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
4

1 に答える 1

0

これは完全に機能しました:

 sun <- read_table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat")
于 2016-04-28T13:30:29.680 に答える