Reading null values ​​from a file

I need to read a data frame from a file containing NULL values. Here is an example file:

charCol floatCol intCol
a 1.5 10
b NULL 3
c 3.9 NULL
d -3.4 4

I read this file in a data frame:

> df <- read.table('example.dat', header=TRUE)

But the "NULL" entries are not interpreted by R as NULL:

> is.null(df$floatCol[2])
[1] FALSE

How do I format an input file so that R correctly processes entries like NULL?

+5
source share
3 answers

Always always always do a summary (thing) if something is unexpected.

> 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  

which looks a little strange. Expand:

> summary(df$floatCol)
 1.5 -3.4  3.9 NULL 
   1    1    1    1 

What the hell is this?

> class(df$floatCol)
[1] "factor"

( "NULL" ) R ", , , ( )".

na.string = "NULL", , NA - , NULL R. 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

, is.na(foo)

+8

:

> 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
+6

r, , "NULL" , , "NULL" . is.null(), "NULL" NULL.

-2

All Articles