Hi, maybe somebody would be kind enough to help a bloody and unprofessional beginner like me (and I hope I did not overlook the answer to my question on the website). I've imported a csv data frame into R, but I can't run a regression because R interprets 4 out of 5 variables as factors (rather than numeric vectors). I tried the as.numeric() command, but R says it is invalid to change the storage mode of a factor. Is there any way to change the mode of a factor into numeric. I am very grateful for help. Thank you very much, Robert. -- View this message in context: http://r.789695.n4.nabble.com/wrong-data-interpretation-in-R-tp4650849.html Sent from the R help mailing list archive at Nabble.com.
Most likely there's something amiss in your csv file: R won't convert numeric data to factors unless there are non-numeric characters included. First check your csv file for errors. If that doesn't solve your problem, please provide a reproducible example. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Sarah On Mon, Nov 26, 2012 at 10:59 AM, EcoFranc <robrit at gmx.de> wrote:> Hi, > > maybe somebody would be kind enough to help a bloody and unprofessional > beginner like me (and I hope I did not overlook the answer to my question on > the website). I've imported a csv data frame into R, but I can't run a > regression because R interprets 4 out of 5 variables as factors (rather than > numeric vectors). I tried the as.numeric() command, but R says it is invalid > to change the storage mode of a factor. Is there any way to change the mode > of a factor into numeric. > > I am very grateful for help. Thank you very much, Robert. > >-- Sarah Goslee http://www.functionaldiversity.org
Robert -- it may be that some of your factor variables cannot be coerced to numeric because they have non-numeric elements. This is why we ask for a reproducible example when posting. I suspect just a few lines from the data frame would tell the story. On 26-Nov-12, at 7:59 AM, EcoFranc wrote:> Hi, > > maybe somebody would be kind enough to help a bloody and > unprofessional > beginner like me (and I hope I did not overlook the answer to my > question on > the website). I've imported a csv data frame into R, but I can't run a > regression because R interprets 4 out of 5 variables as factors > (rather than > numeric vectors). I tried the as.numeric() command, but R says it > is invalid > to change the storage mode of a factor. Is there any way to change > the mode > of a factor into numeric. > > I am very grateful for help. Thank you very much, Robert. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/wrong- > data-interpretation-in-R-tp4650849.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.Don McKenzie, Research Ecologist Pacific Wildland Fire Sciences Lab US Forest Service phone: 206-732-7824 Affiliate Professor School of Environmental and Forest Sciences University of Washington
How to change a factor to numeric is FAQ 7.10 Also look at the `colClasses` argument to read.table/read.csv for a way to specify the type of the column when you are reading it in. On Mon, Nov 26, 2012 at 8:59 AM, EcoFranc <robrit@gmx.de> wrote:> Hi, > > maybe somebody would be kind enough to help a bloody and unprofessional > beginner like me (and I hope I did not overlook the answer to my question > on > the website). I've imported a csv data frame into R, but I can't run a > regression because R interprets 4 out of 5 variables as factors (rather > than > numeric vectors). I tried the as.numeric() command, but R says it is > invalid > to change the storage mode of a factor. Is there any way to change the mode > of a factor into numeric. > > I am very grateful for help. Thank you very much, Robert. > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/wrong-data-interpretation-in-R-tp4650849.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org 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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]