Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing NA's with asterisks (*)

Tags:

replace

r

I have a data set which I read from my a .csv file. After some editing work, I now need to write it as a .txt file with (i) all quotes removed (ii) every NA in the whole file to be replaced by an asterisk, *, and (iii) first row (variable names) excluded.

The following scripts, so far, remove quotes and am just wondering how can (ii) and (iii) can be added or may be done.

write.table(ped5, "ped5.txt" ,row.names=FALSE)
ped5 <- read.table("ped5.txt", header=TRUE)
write.table(ped5, "ped7.txt", row.names=FALSE, quote=FALSE)

Any help would be very much appreciated!

like image 668
baz Avatar asked Mar 03 '26 03:03

baz


1 Answers

For (ii) use na = "*" and for (iii) col.names=FALSE.
So:

write.table(ped5,"ped7.txt",row.names=FALSE,quote=FALSE, na="*", col.names=FALSE)

It's all to find on help page to write.table (?write.table).

like image 84
Marek Avatar answered Mar 04 '26 16:03

Marek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!