Hi, I'm using read.csv to import a table. But sevel columns are changed to factor variables automatically. They are actually numbers not factor levels. Why this happened? How can I get the correct table? Thanks a lot. Sonia [[alternative HTML version deleted]]
Hi Sonia, This suggests that those columns have something in them that is not numeric (e.g., "." to represent missing data, text, etc.). You can avoid them being converted to factor using: read.csv("yourfile.csv", stringsAsFactors = FALSE) however, they will still be character class, not numeric. You will then either need to use as.numeric() on those columns, or figure out what is making R think those are character columns in the first place and correct that in your CSV file. Cheers, Josh On Thu, Oct 21, 2010 at 7:17 PM, mou sonia <paperhome at gmail.com> wrote:> Hi, > > I'm using read.csv to import a table. But sevel columns are changed to > factor variables automatically. They are actually numbers not factor levels. > Why this happened? How can I get the correct table? Thanks a lot. > > Sonia > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
An example would be useful. Look at what is causing what you think are numbers to be interpreted as character strings and therefore being changed to numbers. For example, are there commas in the numbers, are some missing and replaced by some character sequence that represents missing values. You can always convert the column to numerics: yourData$col <- as.numeric(as.character(yourData$col)) So there is something in your data that is causing the conversion. After you do the conversion above, look for NAs in the data. This might show you were the problem is. On Thu, Oct 21, 2010 at 10:17 PM, mou sonia <paperhome at gmail.com> wrote:> Hi, > > I'm using read.csv to import a table. But sevel columns are changed to > factor variables automatically. They are actually numbers not factor levels. > Why this happened? How can I get the correct table? Thanks a lot. > > Sonia > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?