简体   繁体   English

读取csv文件时使用colClasses时R中的警告消息

[英]Warning message in R when using colClasses when reading csv files

I am using lapply to read a list of files. 我正在使用lapply来读取文件列表。 The files have multiple rows and columns, and I interested in the first row in the first column. 这些文件具有多个行和列,我对第一列的第一行感兴趣。 The code I am using is: 我使用的代码是:

lapply(file_list, read.csv,sep=',', header = F, col.names=F, nrow=1, colClasses = c('character', 'NULL', 'NULL'))

The first row has three columns but I am only reading the first one. 第一行有三列,但我只读第一列。 From other posts on stackoverflow I found that the way to do this would be to use colClasses = c('character', 'NULL', 'NULL') . 从关于stackoverflow的其他文章中,我发现执行此操作的方法是使用colClasses = c('character', 'NULL', 'NULL') While this approach is working, I would like to know the underlying issue that is causing the following error message to be generated and hopefully prevent it from popping up: 在这种方法有效的同时,我想知道导致以下错误消息生成的根本问题,并希望防止它弹出:

"In read.table(file = file, header = header, sep = sep, quote = quote, : cols = 1 != length(data) = 3" “在read.table(file = file,header = header,sep = sep,quote = quote,:cols = 1!= length(data)= 3“

It's to let you know that you're just keeping one column of the data out of three because it doesn't know how to handle colClasses of "NULL" . 让您知道您只是将数据中的一列保留为三列,因为它不知道如何处理"NULL" colClasses Note your NULL is in quotation marks. 请注意,您的NULL用引号引起来。

An example: 一个例子:

write.csv(data.frame(fi=letters[1:3],
                            fy=rnorm(3,500,1),
                            fo=rnorm(3,50,2))
,file="a.csv",row.names = F)

write.csv(data.frame(fib=letters[2:4],
                     fyb=rnorm(3,5,1),
                     fob=rnorm(3,50,2))
          ,file="b.csv",row.names = F)

file_list=list("a.csv","b.csv")

lapply(file_list, read.csv,sep=',', header = F, col.names=F, nrow=1, colClasses = c('character', 'NULL', 'NULL'))

Which results in: 结果是:

[[1]]
  FALSE.
1     fi

[[2]]
  FALSE.
1    fib

Warning messages:
1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  cols = 1 != length(data) = 3

Which is the same as if you used: 与您使用的相同:

lapply(file_list, read.csv,sep=',', header = F, col.names=F,
 nrow=1, colClasses = c('character', 'asdasd', 'asdasd'))

But the warning goes away (and you get the rest of the row as a result) if you do: 但是,如果您这样做了,警告就会消失(结果是该行的其余部分):

lapply(file_list, read.csv,sep=',', header = F, col.names=F,
  nrow=1, colClasses = c( 'character',NULL, NULL))

You can see where errors and warnings come from in source code for a function by entering, for example, read.table directly without anything following it, then searching for your particular warning within it. 您可以查看功能源代码中的错误和警告,例如直接输入read.table而不跟随其后,然后在其中搜索特定的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用read.table.ffdf将很大的csv文件读入R时,如何指定colClasses? - How to specify colClasses when reading a very big csv file into R using read.table.ffdf? 在 R 中使用 cbin() 时的警告消息 - Warning message when using cbin() in R 在R中使用If(…)函数时的警告消息 - Warning message when using If(…) function in R 在R中使用'na.strings'和'fClass'参数读取数据时错误的列模式 - Wrong columns' modes when reading data with 'na.strings' and 'colClasses' arguments of 'fread' function in R 尝试从csv文件中选择某些列时出现R colClasses问题 - Issue with R colClasses when trying to select certain columns from csv file 使用R读取多个cs3中的csv文件并将它们组合为单个文件 - reading multiple csv files in s3 and combined them as a single file when the name of files are different using R R 使用 fread colClasses 或跳过参数来读取没有列标题的 csv - R using fread colClasses or skip arguments to read csv with no column headers R中使用带有colClasses参数的'read.csv'comamnd的错误 - Error in R using 'read.csv' comamnd with colClasses argument 使用if()时的警告消息 - Warning Message when using if() 使用list.files将数据读入r时包含.csv文件名 - Include .csv filename when reading data into r using list.files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM