Re: [R] about data structure
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 liwrote: >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@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. __ R-help@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.
Re: [R] about data structure
Hello, Try as.numeric(as.character(DF$precip)) Hope this helps, Rui Barradas Em 07-11-2016 20:24, lily li escreveu: 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@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. __ R-help@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.
[R] about data structure
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@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.