Dear Help-Rs,
I've been dealing with this problem for some time, using a work-around to deal
with it. It's time for me to come clean with my ineptitude and seek a what has
got to be a more streamlined solution from the Help-Rverse.
I regularly import delimited text data that contains numerics enclosed in
quotes (e.g., "00765288071"). Thing is, for some of these data, I need to keep
the values as "character" class within the data frame (that is to say the
leading zeros are important and I would like them to stay). Here is an example
of the code I would use to read an example dataset in question:
mydata <- read.csv("~/mydata.csv", quote = "\"'")
The problem is, when R reads the data and converts them into a data frame,
inevitably, R ignores the quotes around values like the above, and reads them
in as "numeric". So R strips the valuable leading zeros and converts my
"00765288071" to 765288071. I've developed a work-arounds to this involving
the use of the following:
> whatIneed <- "00000000000"
> whatIgot <- 765288071
> whatIgot <- as.character(whatIgot)
> substr(whatIneed, 1+nchar(whatIneed)-nchar(whatIgot), nchar(whatIneed)) <-
> whatIgot
> whatIneed
[1] "00765288071"
My question is, am I missing something in how I'm writing my read.csv statement
that would indicate to R that numerics enclosed in quotes should be read and
imported as characters and not converted to numerics???
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.