首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R数据清理失败:.dat to清理数据帧以清理CSV文件

R数据清理失败:.dat to清理数据帧以清理CSV文件
EN

Stack Overflow用户
提问于 2016-04-26 23:27:10
回答 1查看 169关注 0票数 0

正在将.dat文件导入R控制台。希望得到一个干净的表,这样我就可以把它转换回CSV,并在Excel中操作其他CSV文件。

下面是整个R控制台会话。该表在控制台中看起来是对齐的,但导出到CSV时肯定不是对齐的。试一试代码,看看会发生什么。

代码语言:javascript
复制
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")

第二次尝试:

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

发布于 2016-04-28 21:30:30

这样做效果很好:

代码语言:javascript
复制
 sun <- read_table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36869347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档