[R] about data structure
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Mon Nov 7 23:34:01 CET 2016
You have non-numeric data somewhere in that column, and factor is already a numeric type (Read about factors in the Introduction to R document that comes with R). Try
DF$prec_new = as.numeric(as.character (DF$precip)
and then look at
DF$precip[ is.na( DF$precip_new ) ]
to see which values are corrupt. Eliminating the whole factor conversion in the first place might be as simple as adding an na.string argument to your read.table or read.csv call that imports the data.
If you need further help, next time send plain text format email with a reproducible example.
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
http://adv-r.had.co.nz/Reproducibility.html
--
Sent from my phone. Please excuse my brevity.
On November 7, 2016 12:24:57 PM PST, lily li <chocold12 at gmail.com> wrote:
>Hi R users,
>
>I'm wondering why the values changed when I try to transform factors
>into
>numerics.
>
>For example, for a data frame DF, there is one column called
>precipitation,
>which is usually lower than 100mm. But this column is factor when I
>read in
>the data. When transform to numeric values, some rows can be higher
>than
>1000mm. What is the problem? Thanks.
>
>DF$prec_new = as.numeric(DF$precip)
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
More information about the R-help
mailing list