I have a .csv file with account codes in the form of 00xxxxx and I need them to stay that way for use with other programs which use the account codes in this format. I was just working on an R script to reconcile account charges on Friday and swore that as.is = T
was working for me. Now, it doesn't seem to be. Here's some example data:
test <- data.frame(col1 = c("apple", "banana", "carrot"),
col2 = c(100, 200, 300),
col3 = c("00234", "00345", "00456"))
My write.table
strategy:
write.table(test, file = "C:/path/test.csv", quote = T,
sep=",", row.names = F)
Remove the old data.frame
and re-read:
rm(test)
test <- read.csv("C:/path/test.csv")
test
col1 col2 col3
1 apple 100 234
2 banana 200 345
3 carrot 300 456
In case it's not clear, it should look like the original data.frame
we created:
test
col1 col2 col3
1 apple 100 00234
2 banana 200 00345
3 carrot 300 00456
I also tried the following, after perusing the available read.table
options, with the results all the same as above:
test <- read.csv("C:/path/test.csv", quote = '"')
test <- read.csv("C:/path/test.csv", as.is = T)
test <- read.csv("C:/path/test.csv", as.is = T, quote = '"')
StringsAsFactors
didn't seem to be relevant in this case (and sounds like as.is
will do the same thing.
When I open the file in Emacs, col3
is, indeed, surrounded by quotes, so I'd expect it to be treated like text instead of converted to a number:
Most of the other questions are simply about not treating things like factors, or getting numbers not to be recognized as characters, usually the result of an overlooked character string in that column.
I see I can pursue the colClasses
argument from questions like this one, but I'd prefer not to; my "colClasses" are built into the data :) Quoted = character, not quoted = numeric.