简体   繁体   中英

How do I export a df as.character in R?

How do I export a data frame completely as.character in r? I have digits that need to be treated as text in large dataframes, and I'm using write.csv, but even though I imported digits into r as characters, they are exporting as numbers (not surrounded by"" when viewed in notepad) and are occasionally rewritten as, eg, 1e-04 (for a small decimal value). This is for data munging, and I need stuff to stay as formatted (once formatted). Shouldn't that be possible with some form of "as.character" or similar?

Make it into a matrix. If there is at least one character column in your data frame, it'll coerce the rest to character to match, since you can only have on type of data in a matrix.

new <- as.matrix(old_data_frame)

If there are no character columns in your old data frame, do:

new <- matrix(as.character(as.numeric(as.matrix(old_data_frame))),
     ncol=ncol(old_data_frame))

If you user the function

write.table(x, file= ,quote=TRUE ...)

anything that is a string will be quoted on output

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