Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: https://stat.ethz.ch/pipermail/r-help/attachments/20070812/6da70dc8/attachment.pl
See the FAQ Q7.10 (and please study the posting guide) On Sun, 12 Aug 2007, Falk Lieder wrote:> Hi, > > I have imported a data file to R. Unfortunately R has interpreted some > numeric variables as factors. Therefore I want to reconvert these to numeric > vectors whose values are the factor levels' labels. I tried > as.numeric(<factor>), > but it returns a vector of factor levels (i.e. 1,2,3,...) instead of labels > (i.e. 0.71, 1.34, 2.61,).> What can I do instead? > > Best wishes, Falk > > [[alternative HTML version deleted]]> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
This is one of R's rather _endearing_ little idiosyncrasies. I ran into it a while ago. http://finzi.psych.upenn.edu/R/Rhelp02a/archive/98090.html For some reason, possibly historical, the option "stringAsFactors" is set to TRUE. As Prof Ripley says FAQ 7.10 will tell you as.numeric(as.character(f)) # for a one-off conversion>From Gabor Grothendieck A one-off solution for acomplete data.frame DF <- data.frame(let = letters[1:3], num = 1:3, stringsAsFactors = FALSE) str(DF) # to see what has happened. You can reset the option globally, see below. However you might want to read Gabor Grothendieck's comment about this in the thread referenced above since it could cause problems if you transfer files alot. Personally I went with the global option since I don't tend to transfer programs to other people and I was getting tired of tracking down errors in my programs caused by numeric and character variables suddenly deciding to become factors.>From Steven Tucker:You can also this option globally with options(stringsAsFactors = TRUE) # in \library\base\R\Rprofile --- Falk Lieder <falk.lieder at googlemail.com> wrote:> Hi, > > I have imported a data file to R. Unfortunately R > has interpreted some > numeric variables as factors. Therefore I want to > reconvert these to numeric > vectors whose values are the factor levels' labels. > I tried > as.numeric(<factor>), > but it returns a vector of factor levels (i.e. > 1,2,3,...) instead of labels > (i.e. 0.71, 1.34, 2.61,).> What can I do instead? > > Best wishes, Falk