user1631306 user1631306 - 2 months ago 16
R Question

read.csv blank fields to NA

I have tab delimited text file, named 'a.txt'. The D column is empty.

10 20 NaN
30 40
40 30 20
20 NA 20

I want to have the dataframe looking and acting exactly as the text file, with a space in the 2nd row and in the 2nd column.

Unfortunately, read.csv is converting all the blanks and NA to "NA". I want to read NA and NaN as characters.

b<- read.csv("a.txt",sep="\t", skip =0, header = TRUE, comment.char = "",check.names = FALSE, quote="", )

To summarize: I want to replicate the same values in output file without modifying them:

  • If there is a blank in input, the output should be blank.

  • If the input has NA or Nan, then the output should also have NA or NaN.


After reading the csv file, try the following. It will replace the NA values with "".


Fairly certain that won't fix your NaN values. That will need to be resolved in a separate statement