fread中未被识别为NA的空白的解决方法

时间:2018-09-03 22:12:15

标签: r data.table

我最近将data.table版本升级到1.11.4,并且遇到了问题。它与以下问题类似:

Blank space not recognised as NA in fread

但是,在评论中,只是简单地说:“我想这是在data.table版本1.11.0中引入的(请参见github.com/Rdatatable/data.table/blob/master/NEWS.md,第1节)quote:“我们希望,, => NA以保持与数字类型的一致性,并且”“ =>空字符串是fwrite / fread字符列的标准默认值,以便fread(fwrite(DT))== DT无需更改任何参数。”

我已经阅读了下面的文档,并尝试使用“ options(datatable.fread.na.strings =“ NA”)“,但仍将字符列读为空白而不是NA。必须有解决方法吗?

示例:

library(data.table)
x1 <- c("","","")
x2 <- c("1006678566","","1011160152")
x3 <- c("","ac","")
x4 <- c("","2","3")
df <- cbind.data.frame(x1,x2,x3,x4)
write.csv(df,"tr.csv")

tr1 <- fread("tr.csv", header=T, fill = T,
sep= ",", na.strings = c("",NA), data.table = F,stringsAsFactors = FALSE)

tr2 <- read.table("tr.csv", fill = TRUE, header=T, sep= ",", na.strings = c(""," ", NA), stringsAsFactors = FALSE)

0 个答案:

没有答案