Question

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, e.g., 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?

Était-ce utile?

La solution

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))

Autres conseils

If you user the function

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

anything that is a string will be quoted on output

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top