Using variable-length data.table fread file for empty missing values

I have a dataset with many missing values. Some missing values ​​are NA, some are Nulls, and others have different gap lengths. I would like to use the function freadin Rto read all these values ​​as missing.

Here is an example:

#Find fake data
iris <- data.table(iris)[1:5]

#Add missing values non-uniformly
iris[1,Species:='         ']
iris[2,Species:=' ']
iris[3,Species:='NULL']

#Write to csv and read back in using fread
write.csv(iris,file="iris.csv")
fread("iris.csv",na.strings=c("NULL"," "))

   V1 Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1:  1          5.1         3.5          1.4         0.2          
2:  2          4.9         3.0          1.4         0.2        NA
3:  3          4.7         3.2          1.3         0.2        NA
4:  4          4.6         3.1          1.5         0.2    setosa
5:  5          5.0         3.6          1.4         0.2    setosa

In the above example, we see that I can not account for the first missing value, since there are many spaces. Does anyone know a way to account for this?

+4
source share
1 answer

Thanks so much for the great answer from @eddi.

fread("sed 's/ *//g' iris.csv",na.strings=c("",NA,"NULL"))
+4
source

Source: https://habr.com/ru/post/1544378/


All Articles