how to include \"NULL\" as the na.string when importing with R data.table fread

65 Views Asked by At

i'm struggling with how to read the quoted text NULL as an NA string when importing R character columns.. i am just trying to get that 3rd row to be read in as missing, but instead i'm getting the text "NULL"

# simple dummy dataset with six rows and two columns
w <- mtcars
w[ , 'cyl' ] <- as.character( w[ , 'cyl' ] )
w[ 3 , 'cyl' ] <- 'NULL'
w <- w[ 1:6 , c( 'cyl' , 'mpg' ) ]


# temporary filepath
tf1 <- paste0( tempfile() , '.csv' )

# save dummy dataset to disk
data.table::fwrite( w , tf1 , quote = TRUE )

# dummy dataset looks like this:
readLines(tf1)


# both of these work if columns are read as a numeric type
data.table::fread( tf1 , na.strings = '"NULL"' )
data.table::fread( tf1 , na.strings = '\"NULL\"' )


# attempt at reading character column all fail
data.table::fread( tf1 , colClasses = c( 'character' , 'numeric' ) , na.strings = 'NULL' )
data.table::fread( tf1 , colClasses = c( 'character' , 'numeric' ) , na.strings = '"NULL"' )
data.table::fread( tf1 , colClasses = c( 'character' , 'numeric' ) , na.strings = '\"NULL\"' )
data.table::fread( tf1 , colClasses = c( 'character' , 'numeric' ) , na.strings = '\\"NULL\\"' )
data.table::fread( tf1 , colClasses = c( 'character' , 'numeric' ) , na.strings = '`\\"NULL\\"`' )

any ideas about this would be appreciated! thanks

0

There are 0 best solutions below