简体   繁体   中英

dealing with blank/missing data with write.table in R

I have a data frame where some of the rows have blanks entries, eg to use a toy example

Sample Gene RS Chromosome
1      A    rs1 10
2      B        X
3      C    rs4 Y

ie sample 2 has no rs#. If I attempt to save this data frame in a file using:

write.table(mydata,file="myfile",quote=FALSE,sep='\t')

and then read.table('myfile',header=TRUE,sep='\\t') , I get an error stating that the number of entries in line 2 doesn't have 4 elements. If I set quote=TRUE , then a "" entry appears in the table. I'm trying to figure out a way to create a table using write.table with quote=FALSE while retaining a blank placeholder for rows with missing entries such as 2.

Is there a simple way to do this? I attempted to use the argument NA="" in write.table() but this didn't change anything.

If result of my script's data frame has NA I always replace it , One way would be to replace NA in the data frames with a some other text which tells you that this entry was NA in the data frame -Specially if you are saving the result in a csv /database or some non -R env

a simple script to do that

replace_NA <- function(x,replacement="N/A"){
  x[is.na(x)==T]  <- replacement

}

sapply(df,replace_NA,replacement ="N/A" )

You are attempting to reinvent the fixed-width file format. Your requested format would have a blank column between every real column. I don't find a write.fwf , although the 'utils' package has read.fwf . The simplest method of getting your requested output would be:

 capture.output(dat, file='test.dat')
# Result in a text file

  Sample Gene  RS Chromosome
1      1    A rs1         10
2      2    B              X
3      3    C rs4          Y

This essentially uses the print method (at the end of the R REPL) for dataframes to do the spacing for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM