Hi, I have tried to load a file originally from Excel, via csv, text and clipboard today. When I succeed I cannot change the format from "factor", and when I try to convert it to numerical it only gives the position of the "factor-group", not the real value in the column? Any quick suggestions? Andreas [[alternative HTML version deleted]]
Hi Andreas, if your factor is named "x", you can do as.numeric(as.character(x)) Best, Ista On Fri, Mar 11, 2011 at 10:45 AM, Andreas Emanuelsson <Andreas.Emanuelsson at sik.se> wrote:> Hi, I have tried to load a file originally from Excel, via csv, text and clipboard today. > > When I succeed I cannot change the format from "factor", and when I try to convert it to numerical it only gives the position of the "factor-group", not the real value in the column? > > Any quick suggestions? > > Andreas > > > ? ? ? ?[[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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
If you are using 'read.csv', add the parameter as.is=TRUE to prevent conversion to factors. If you have factors that are supposed to be numeric, use the following df$mydata <- as.numeric(as.character(df$mydata)) On Fri, Mar 11, 2011 at 10:45 AM, Andreas Emanuelsson <Andreas.Emanuelsson at sik.se> wrote:> Hi, I have tried to load a file originally from Excel, via csv, text and clipboard today. > > When I succeed I cannot change the format from "factor", and when I try to convert it to numerical it only gives the position of the "factor-group", not the real value in the column? > > Any quick suggestions? > > Andreas > > > ? ? ? ?[[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 Data Munger Guru What is the problem that you are trying to solve?
Hi Andreas, Assuming you are using read.table(), try setting the argument: stringsAsFactors = FALSE Also consider what about the Excel file is making R default to a factor rather than numerical? There may be a nonstandard reprsentation of missing data (e.g., "."), which could also be specified to read.table() For more info, see the documentation for ?read.table HTH, Josh On Fri, Mar 11, 2011 at 7:45 AM, Andreas Emanuelsson <Andreas.Emanuelsson at sik.se> wrote:> Hi, I have tried to load a file originally from Excel, via csv, text and clipboard today. > > When I succeed I cannot change the format from "factor", and when I try to convert it to numerical it only gives the position of the "factor-group", not the real value in the column? > > Any quick suggestions? > > Andreas > > > ? ? ? ?[[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/