R read.csv"比列名更多的列"错误

Vic*_*227 2 import r

.csv文件导入R 时出现问题.我的代码:

t <- read.csv("C:\\N0_07312014.CSV", na.string=c("","null","NaN","X"),
          header=T, stringsAsFactors=FALSE,check.names=F)
Run Code Online (Sandbox Code Playgroud)

R报告错误并且不按我的意愿执行操作:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  more columns than column names
Run Code Online (Sandbox Code Playgroud)

我想问题是因为我的数据格式不正确.我只需要来自的数据[,1:32].应删除所有其他人.

可从以下网址下载数据:https: //drive.google.com/file/d/0B86_a8ltyoL3VXJYM3NVdmNPMUU/edit?usp =sharing

非常感谢!

小智 9

打开 .csv 作为文本文件(例如,在 Mac 上使用 TextEdit)并检查列是否用逗号分隔。

csv 是“逗号分隔的向量”。出于某种原因,当 Excel 保存我的 csv 时,它使用分号代替。

打开 csv 时使用:

read.csv("file_name.csv",sep=";")
Run Code Online (Sandbox Code Playgroud)

分号只是一个例子,但正如之前其他人所建议的那样,不要假设因为你的 csv 在 Excel 中看起来不错,所以它就是这样。


hrb*_*str 7

这是一个不稳定的CSV文件.抛出多个标题(尝试将其粘贴到CSV指纹)以查看我的意思.

由于我不知道数据,因此不可能确定以下内容为您生成准确的结果,但它涉及使用readLines和其他R函数预处理文本:

# use readLines to get the data
dat <- readLines("N0_07312014.CSV")

# i had to do this to fix grep errors
Sys.setlocale('LC_ALL','C')

# filter out the repeating, and wonky headers
dat_2 <- grep("Node Name,RTC_date", dat, invert=TRUE, value=TRUE)

# turn that vector into a text connection for read.csv
dat_3 <- read.csv(textConnection(paste0(dat_2, collapse="\n")),
                  header=FALSE, stringsAsFactors=FALSE)

str(dat_3)
## 'data.frame':    308 obs. of  37 variables:
##  $ V1 : chr  "Node 0" "Node 0" "Node 0" "Node 0" ...
##  $ V2 : chr  "07/31/2014" "07/31/2014" "07/31/2014" "07/31/2014" ...
##  $ V3 : chr  "08:58:18" "08:59:22" "08:59:37" "09:00:06" ...
##  $ V4 : chr  "" "" "" "" ...
## .. more
##  $ V36: chr  "" "" "" "" ...
##  $ V37: chr  "0" "0" "0" "0" ...

# grab the headers
headers <- strsplit(dat[1], ",")[[1]]

# how many of them are there?
length(headers)
## [1] 32

# limit it to the 32 columns you want (Which matches)
dat_4 <- dat_3[,1:32]

# and add the headers
colnames(dat_4) <- headers

str(dat_4)
## 'data.frame':    308 obs. of  32 variables:
##  $ Node Name         : chr  "Node 0" "Node 0" "Node 0" "Node 0" ...
##  $ RTC_date          : chr  "07/31/2014" "07/31/2014" "07/31/2014" "07/31/2014" ...
##  $ RTC_time          : chr  "08:58:18" "08:59:22" "08:59:37" "09:00:06" ...
##  $ N1 Bat (VDC)      : chr  "" "" "" "" ...
##  $ N1 Shinyei (ug/m3): chr  "" "" "0.23" "null" ...
##  $ N1 CC (ppb)       : chr  "" "" "null" "null" ...
##  $ N1 Aeroq (ppm)    : chr  "" "" "null" "null" ...
## ... continues
Run Code Online (Sandbox Code Playgroud)


mgr*_*ebe 5

如果您只需要前 32 列,并且您知道有多少列,则可以将其他列类设置为 NULL。

read.csv("C:\\N0_07312014.CSV", na.string=c("","null","NaN","X"),
      header=T, stringsAsFactors=FALSE,
      colClasses=c(rep("character",32),rep("NULL",10)))
Run Code Online (Sandbox Code Playgroud)

如果您不想编码每个 colClass 并且您喜欢猜测,read.csv那么只需保存该 csv 并再次打开它。

或者,您可以跳过标题并自己命名列并删除行为不当的行。

A<-data.frame(read.csv("N0_07312014.CSV",
                        header=F,stringsAsFactors=FALSE,
                        colClasses=c(rep("character",32),rep("NULL",5)),
                        na.string=c("","null","NaN","X")))
Yournames<-as.character(A[1,])
names(A)<-Yournames
yourdata<-unique(A)[-1,]
Run Code Online (Sandbox Code Playgroud)

上面的代码假设您不想要任何重复的行。您也可以删除第一个条目等于第一个列名的行,但我将把它留给您。