简体   繁体   中英

write.table converting NaNs to NA

I have a data frame

a = data.frame("a"=c(1,2,3,NA,NaN,6),"b"=c(NA,NA,NaN,1,2,3),"c"=c(1,2,NA,NA,5,6))

I am writing it to a file

write.table(a,file="t.txt",row.names=FALSE,quote=FALSE,sep="\t")

Its converting "NaN" to "NA". I want to retain the original values. How can I do that? I want to keep NaN and NA in my t.txt. But I get all NA in my t.txt

I'm not sure there's a way to do it directly, but you can always convert the columns to character, then write it (since you're using quote=FALSE ).

> a[] <- lapply(a, as.character)
> write.table(a,file="t.txt",row.names=FALSE,quote=FALSE,sep="\t")
> str(read.table("t.txt",header=TRUE))
'data.frame':   6 obs. of  3 variables:
 $ a: num  1 2 3 NA NaN 6
 $ b: num  NA NA NaN 1 2 3
 $ c: int  1 2 NA NA 5 6

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