我需要从包含NULL值的文件中读取数据帧.这是一个示例文件:
charCol floatCol intCol a 1.5 10 b NULL 3 c 3.9 NULL d -3.4 4
我把这个文件读入数据框:
> df <- read.table('example.dat', header=TRUE)
Run Code Online (Sandbox Code Playgroud)
但是"NULL"条目不会被R解释为NULL:
> is.null(df$floatCol[2])
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
我应该如何格式化我的输入文件,以便R正确地将这些条目视为NULL?
如果出现意外情况,总是总是做汇总(事情).
> summary(df)
charCol floatCol intCol
a:1 1.5 :1 10 :1
b:1 -3.4:1 3 :1
c:1 3.9 :1 4 :1
d:1 NULL:1 NULL:1
Run Code Online (Sandbox Code Playgroud)
看起来有点奇怪.深入研究:
> summary(df$floatCol)
1.5 -3.4 3.9 NULL
1 1 1 1
Run Code Online (Sandbox Code Playgroud)
到底是什么?
> class(df$floatCol)
[1] "factor"
Run Code Online (Sandbox Code Playgroud)
存在无效的数字格式(字符串'NULL')导致R变为"哦,我猜这些不是数字,我会将它们读入字符串并为您制作一个因子(分类变量)".
该解决方案刚刚发布使用na.string ="NULL",但请记住,NA与R中的NULL不同.NA是缺失数据的标记,NULL是真正的非值.相比:
> c(1,2,3,NULL,4)
[1] 1 2 3 4
> c(1,2,3,NA,4)
[1] 1 2 3 NA 4
Run Code Online (Sandbox Code Playgroud)
一旦你正确阅读它,适当的测试通常是is.na(foo)
试试这个:
> Lines <- "charCol floatCol intCol
+ a 1.5 10
+ b NULL 3
+ c 3.9 NULL
+ d -3.4 4"
>
> # DF <- read.table("myfile", header = TRUE, na.strings = "NULL")
> DF <- read.table(textConnection(Lines), header = TRUE, na.strings = "NULL")
> DF
charCol floatCol intCol
1 a 1.5 10
2 b NA 3
3 c 3.9 NA
4 d -3.4 4
Run Code Online (Sandbox Code Playgroud)