[R] / Operator not meaningful for factors

Petr PIKAL petr.pikal at precheza.cz
Tue May 4 10:12:42 CEST 2010


Hi

"Katya Mauff" <Katya.Mauff at uct.ac.za> napsal dne 04.05.2010 09:53:49:

> another way to do it (if you are still having problems) is to use sapply
> (yourdataname,data.class) after you've read it in, which will tell you 
the 
> data class of each of your variables (factor, numeric etc). You can then 

> change your factor variables to numeric by typing yourdataname
> $variablename=as.numeric(yourdataname$variablename).

You can not!!!!! When you do it you will get numeric **representation** of 
factor levels.

> x<-rnorm(5)
> x
[1] -1.1550254 -0.4578429 -0.6089007 -1.7378599  0.1297853
> x.f<-factor(x)
> x.f
[1] -1.15502538070463  -0.457842907389024 -0.608900689299125 
-1.73785992416606 
[5] 0.129785271221269 
5 Levels: -1.73785992416606 -1.15502538070463 ... 0.129785271221269
> as.numeric(x.f)
[1] 2 4 3 1 5
>

Regards
Petr



> 
> >>> Petr PIKAL <petr.pikal at precheza.cz> 2010/05/04 09:38 AM >>>
> Hi
> 
> r-help-bounces at r-project.org napsal dne 04.05.2010 00:50:00:
> 
> >  I think that you are correct.  R has the annoying habit of converting 

> > character data to factors when you don't want it to while it is 
> importing 
> > data.  This is because the in the option "stringsAsFactors" is set to 
> TRUE for
> > some weird historical reasons.
> 
> It is a matter of opinion. I consider it quite useful feature. If I see 
by
> 
> str(some.data) or summary(data0 that numeric columns are factors I know 
> something is wrong with input.
> 
> and when I want to use ggplot, xyplot or just plot my data with 
different 
> colours/sizes/pchs/.... it is quite easy to use as.numeric(my.factor) to 

> get numeric representation of levels.
> 
> Finally you can easily change labels, concatenate levels and so on.
> 
> Just my 2 cents.
> 
> Regards
> Petr
> 
> 
> 
> > 
> > Try the command str(insert name of data) and see what happens.  It 
> should show
> > you which columns of data are being treated as factors.
> > 
> > You can convert the back to character or to numeric.  See the FAQ Part 
7 
> "How 
> > do I convert factors to numeric? " or you can use the String as 
options 
> > command in the read.table to FALSE
> > 
> > Something like this should work, I think, but it's not tested
> > read.table("C:/rdata/trees.csv", stringsAsFactors=FALSE)
> > 
> > 
> > 
> > 
> > 
> > --- On Mon, 5/3/10, vincent.deluard <vincent.deluard at trimtabs.com> 
> wrote:
> > 
> > > From: vincent.deluard <vincent.deluard at trimtabs.com>
> > > Subject: Re: [R] / Operator not meaningful for factors
> > > To: r-help at r-project.org
> > > Received: Monday, May 3, 2010, 6:22 PM
> > > 
> > > Hi there,
> > > 
> > > This will sound very stupid because I just started using R
> > > but I see you had
> > > similar problems.
> > > 
> > > I just loaded a very large dataset (2950*6602) from csv
> > > into R. The format
> > > is ticker=row, date=column.
> > > Every time I want to compute basic operations, R returns
> > > "In Ops.factor: not
> > > meaningful for factors"
> > > 
> > > I believe it is because R does not read the data as numbers
> > > but I am not
> > > sure. Can anybody help?
> > > 
> > > Thanks! 
> > > -- 
> > > View this message in context: 
> http://r.789695.n4.nabble.com/Operator-not-
> > meaningful-for-factors-tp791563p2124697.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.
> > >
> > 
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.

> 
______________________________________________________________________________________________
> UNIVERSITY OF CAPE TOWN 
> This e-mail is subject to the UCT ICT policies and e-mail disclaimer 
published
> on our website at http://www.uct.ac.za/about/policies/emaildisclaimer/ 
or 
> obtainable from +27 21 650 4500. This e-mail is intended only for the 
person
> (s) to whom it is addressed. If the e-mail has reached you in error, 
please 
> notify the author. If you are not the intended recipient of the e-mail 
you may
> not use, disclose, copy, redirect or print the content. If this e-mail 
is not 
> related to the business of UCT it is sent by the sender in the sender's 
> individual capacity. 
>



More information about the R-help mailing list