[英]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.