/ Operator not meaningful for factors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

/ Operator not meaningful for factors

Vivek Satsangi
Folks,
I have a very basic question. The solution eludes me perhaps because
of my own lack of creativity. I am not attaching a fully reproducible
session because the issue may well be becuase of the way the data file
is, and the data file is large (and I don't know whether I can legally
distribute it). If people can suggest things that might be wrong in my
data or the way that I am reading it, I would be most grateful.

I get the following error message in the session quoted at the end of
this email:
/ not meaningful for factors in: Ops.factor(BookValuePS, Price)

As you can see in that some session, I check that the two vectors
being divided are numeric. I also check that the divisor is not 0 at
any index. I also believe that this is not because of the NA's in the
data. My question is, what are other "problems" that can cause the /
operator to not be meaningful?

I did try some simple examples to try to get the same error. However,
I am not sure how to put the same NA's that one  gets from
read.table() into a vector:
> a <- c(1, 2, 3, NA);
> a
[1]  1  2  3 NA
> b <- c( 1, 2, 3, 4);
> c <- b / a;
> b
[1] 1 2 3 4
> a <- c(1, 2, 3, );
> c <- b/a;
Warning message:
longer object length
        is not a multiple of shorter object length in: b/a


******** Quoted Session below ********
 > explainPriceSimplified <- read.table("combinedClean.csv",
+                            sep = ",", header=TRUE);
> attach(explainPriceSimplified);
> summary(explainPriceSimplified);
     Symbol           Date              Price            EPS          
   BookValuePS
 XL     :   98   Min.   :19870630   22     :   61   Min.   :-1.401e+05
  Min.   :-6.901e+05
 ZION   :   97   1st Qu.:19910930   26.5   :   61   1st Qu.: 4.650e-01
  1st Qu.: 3.892e+00
 YRCW   :   72   Median :19960331   27.5   :   58   Median : 1.060e+00
  Median : 7.882e+00
 AA     :   71   Mean   :19957688   30     :   58   Mean   :-1.534e+01
  Mean   : 1.515e+02
 ABS    :   71   3rd Qu.:20001231   25     :   56   3rd Qu.: 1.890e+00
  3rd Qu.: 1.444e+01
 ABT    :   71   Max.   :20041231   (Other):29561   Max.   : 5.309e+03
  Max.   : 3.366e+06
 (Other):29624                      NA's   :  249   NA's   : 2.460e+02
  NA's   : 4.760e+02
 FiscalQuarterRep    F12MRet
 2004/2F:  482    Min.   :-100.00
 2003/4F:  471    1st Qu.:  -8.82
 2004/1F:  470    Median :  10.57
 2004/3F:  470    Mean   :  13.36
 2003/3F:  464    3rd Qu.:  31.12
 2003/2F:  463    Max.   :4700.00
 (Other):27284    NA's   : 463.00
> mode(Price)
[1] "numeric"
> mode(EPS)
[1] "numeric"
> mode(BookValuePS)
[1] "numeric"
> BP <- BookValuePS / Price ;
Warning message:
/ not meaningful for factors in: Ops.factor(BookValuePS, Price)
> which(Price==0)
numeric(0)
>


--
-- Vivek Satsangi
Student, Rochester, NY USA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

Gabor Grothendieck
But factors are numeric:

mode(factor(1:3)) # numeric

so the numerator or denominator are likely a factor.
Try using str and class rather than mode to investigate
this.


On 1/15/06, Vivek Satsangi <[hidden email]> wrote:

> Folks,
> I have a very basic question. The solution eludes me perhaps because
> of my own lack of creativity. I am not attaching a fully reproducible
> session because the issue may well be becuase of the way the data file
> is, and the data file is large (and I don't know whether I can legally
> distribute it). If people can suggest things that might be wrong in my
> data or the way that I am reading it, I would be most grateful.
>
> I get the following error message in the session quoted at the end of
> this email:
> / not meaningful for factors in: Ops.factor(BookValuePS, Price)
>
> As you can see in that some session, I check that the two vectors
> being divided are numeric. I also check that the divisor is not 0 at
> any index. I also believe that this is not because of the NA's in the
> data. My question is, what are other "problems" that can cause the /
> operator to not be meaningful?
>
> I did try some simple examples to try to get the same error. However,
> I am not sure how to put the same NA's that one  gets from
> read.table() into a vector:
> > a <- c(1, 2, 3, NA);
> > a
> [1]  1  2  3 NA
> > b <- c( 1, 2, 3, 4);
> > c <- b / a;
> > b
> [1] 1 2 3 4
> > a <- c(1, 2, 3, );
> > c <- b/a;
> Warning message:
> longer object length
>        is not a multiple of shorter object length in: b/a
>
>
> ******** Quoted Session below ********
>  > explainPriceSimplified <- read.table("combinedClean.csv",
> +                            sep = ",", header=TRUE);
> > attach(explainPriceSimplified);
> > summary(explainPriceSimplified);
>     Symbol           Date              Price            EPS
>   BookValuePS
>  XL     :   98   Min.   :19870630   22     :   61   Min.   :-1.401e+05
>  Min.   :-6.901e+05
>  ZION   :   97   1st Qu.:19910930   26.5   :   61   1st Qu.: 4.650e-01
>  1st Qu.: 3.892e+00
>  YRCW   :   72   Median :19960331   27.5   :   58   Median : 1.060e+00
>  Median : 7.882e+00
>  AA     :   71   Mean   :19957688   30     :   58   Mean   :-1.534e+01
>  Mean   : 1.515e+02
>  ABS    :   71   3rd Qu.:20001231   25     :   56   3rd Qu.: 1.890e+00
>  3rd Qu.: 1.444e+01
>  ABT    :   71   Max.   :20041231   (Other):29561   Max.   : 5.309e+03
>  Max.   : 3.366e+06
>  (Other):29624                      NA's   :  249   NA's   : 2.460e+02
>  NA's   : 4.760e+02
>  FiscalQuarterRep    F12MRet
>  2004/2F:  482    Min.   :-100.00
>  2003/4F:  471    1st Qu.:  -8.82
>  2004/1F:  470    Median :  10.57
>  2004/3F:  470    Mean   :  13.36
>  2003/3F:  464    3rd Qu.:  31.12
>  2003/2F:  463    Max.   :4700.00
>  (Other):27284    NA's   : 463.00
> > mode(Price)
> [1] "numeric"
> > mode(EPS)
> [1] "numeric"
> > mode(BookValuePS)
> [1] "numeric"
> > BP <- BookValuePS / Price ;
> Warning message:
> / not meaningful for factors in: Ops.factor(BookValuePS, Price)
> > which(Price==0)
> numeric(0)
> >
>
>
> --
> -- Vivek Satsangi
> Student, Rochester, NY USA
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

Prof Brian Ripley
In reply to this post by Vivek Satsangi
The mode of a factor is numeric, so your test does not do what you think
it does.

is.numeric() is the recommended test of a vector being numeric.  I have no
idea where you got the idea that mode() was a useful test (perhaps you
could give us the reference you used), but it rather rarely is (typeof is
usually more informative).

>From the summary quoted, Price is clearly a factor.  Test it with
is.factor.

On Sun, 15 Jan 2006, Vivek Satsangi wrote:

> Folks,
> I have a very basic question. The solution eludes me perhaps because
> of my own lack of creativity. I am not attaching a fully reproducible
> session because the issue may well be becuase of the way the data file
> is, and the data file is large (and I don't know whether I can legally
> distribute it). If people can suggest things that might be wrong in my
> data or the way that I am reading it, I would be most grateful.
>
> I get the following error message in the session quoted at the end of
> this email:
> / not meaningful for factors in: Ops.factor(BookValuePS, Price)
>
> As you can see in that some session, I check that the two vectors
> being divided are numeric.

(see the request above for your reference here)

> I also check that the divisor is not 0 at any index. I also believe that
> this is not because of the NA's in the data. My question is, what are
> other "problems" that can cause the / operator to not be meaningful?

Why not test for factor, since that is what the very helpful error message
told you the problem was?

> I did try some simple examples to try to get the same error. However,
> I am not sure how to put the same NA's that one  gets from
> read.table() into a vector:
>> a <- c(1, 2, 3, NA);
>> a
> [1]  1  2  3 NA
>> b <- c( 1, 2, 3, 4);
>> c <- b / a;
>> b
> [1] 1 2 3 4
>> a <- c(1, 2, 3, );
>> c <- b/a;
> Warning message:
> longer object length
>        is not a multiple of shorter object length in: b/a
>
>
> ******** Quoted Session below ********
> > explainPriceSimplified <- read.table("combinedClean.csv",
> +                            sep = ",", header=TRUE);
>> attach(explainPriceSimplified);
>> summary(explainPriceSimplified);
>     Symbol           Date              Price            EPS
>   BookValuePS
> XL     :   98   Min.   :19870630   22     :   61   Min.   :-1.401e+05
>  Min.   :-6.901e+05
> ZION   :   97   1st Qu.:19910930   26.5   :   61   1st Qu.: 4.650e-01
>  1st Qu.: 3.892e+00
> YRCW   :   72   Median :19960331   27.5   :   58   Median : 1.060e+00
>  Median : 7.882e+00
> AA     :   71   Mean   :19957688   30     :   58   Mean   :-1.534e+01
>  Mean   : 1.515e+02
> ABS    :   71   3rd Qu.:20001231   25     :   56   3rd Qu.: 1.890e+00
>  3rd Qu.: 1.444e+01
> ABT    :   71   Max.   :20041231   (Other):29561   Max.   : 5.309e+03
>  Max.   : 3.366e+06
> (Other):29624                      NA's   :  249   NA's   : 2.460e+02
>  NA's   : 4.760e+02
> FiscalQuarterRep    F12MRet
> 2004/2F:  482    Min.   :-100.00
> 2003/4F:  471    1st Qu.:  -8.82
> 2004/1F:  470    Median :  10.57
> 2004/3F:  470    Mean   :  13.36
> 2003/3F:  464    3rd Qu.:  31.12
> 2003/2F:  463    Max.   :4700.00
> (Other):27284    NA's   : 463.00
>> mode(Price)
> [1] "numeric"
>> mode(EPS)
> [1] "numeric"
>> mode(BookValuePS)
> [1] "numeric"
>> BP <- BookValuePS / Price ;
> Warning message:
> / not meaningful for factors in: Ops.factor(BookValuePS, Price)
>> which(Price==0)
> numeric(0)

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

Vivek Satsangi
Sir,
I made the (incorrect, probably unjustified) deduction of using mode()
based on section 3.1 of "An Introduction to R". Since the write up
talks about the "mode" of an object, and using attr() did not work (it
gives some error saying that "mode of name must be character"), I
tried mode() and reached this incorrect conclusion.

I have had this confusion for a while now about the fact that
something is numeric AND it is a factor, since if it were just a
vector and not a factor, it would still be numeric, as in:
> a <- c (1, 2, 3);
> class(a);
[1] "numeric"

I'll try to think of a way to improve the explanation in "An
Introduction to R" so that the next person coming along does not fall
into the same pit.

Thank you for getting me unstuck,

Vivek

On 1/15/06, Prof Brian Ripley <[hidden email]> wrote:

> The mode of a factor is numeric, so your test does not do what you think
> it does.
>
> is.numeric() is the recommended test of a vector being numeric.  I have no
> idea where you got the idea that mode() was a useful test (perhaps you
> could give us the reference you used), but it rather rarely is (typeof is
> usually more informative).
>
> From the summary quoted, Price is clearly a factor.  Test it with
> is.factor.
>
> On Sun, 15 Jan 2006, Vivek Satsangi wrote:
>
> > Folks,
> > I have a very basic question. The solution eludes me perhaps because
> > of my own lack of creativity. I am not attaching a fully reproducible
> > session because the issue may well be becuase of the way the data file
> > is, and the data file is large (and I don't know whether I can legally
> > distribute it). If people can suggest things that might be wrong in my
> > data or the way that I am reading it, I would be most grateful.
> >
> > I get the following error message in the session quoted at the end of
> > this email:
> > / not meaningful for factors in: Ops.factor(BookValuePS, Price)
> >
> > As you can see in that some session, I check that the two vectors
> > being divided are numeric.
>
> (see the request above for your reference here)
>
> > I also check that the divisor is not 0 at any index. I also believe that
> > this is not because of the NA's in the data. My question is, what are
> > other "problems" that can cause the / operator to not be meaningful?
>
> Why not test for factor, since that is what the very helpful error message
> told you the problem was?
>
> > I did try some simple examples to try to get the same error. However,
> > I am not sure how to put the same NA's that one  gets from
> > read.table() into a vector:
> >> a <- c(1, 2, 3, NA);
> >> a
> > [1]  1  2  3 NA
> >> b <- c( 1, 2, 3, 4);
> >> c <- b / a;
> >> b
> > [1] 1 2 3 4
> >> a <- c(1, 2, 3, );
> >> c <- b/a;
> > Warning message:
> > longer object length
> >        is not a multiple of shorter object length in: b/a
> >
> >
> > ******** Quoted Session below ********
> > > explainPriceSimplified <- read.table("combinedClean.csv",
> > +                            sep = ",", header=TRUE);
> >> attach(explainPriceSimplified);
> >> summary(explainPriceSimplified);
> >     Symbol           Date              Price            EPS
> >   BookValuePS
> > XL     :   98   Min.   :19870630   22     :   61   Min.   :-1.401e+05
> >  Min.   :-6.901e+05
> > ZION   :   97   1st Qu.:19910930   26.5   :   61   1st Qu.: 4.650e-01
> >  1st Qu.: 3.892e+00
> > YRCW   :   72   Median :19960331   27.5   :   58   Median : 1.060e+00
> >  Median : 7.882e+00
> > AA     :   71   Mean   :19957688   30     :   58   Mean   :-1.534e+01
> >  Mean   : 1.515e+02
> > ABS    :   71   3rd Qu.:20001231   25     :   56   3rd Qu.: 1.890e+00
> >  3rd Qu.: 1.444e+01
> > ABT    :   71   Max.   :20041231   (Other):29561   Max.   : 5.309e+03
> >  Max.   : 3.366e+06
> > (Other):29624                      NA's   :  249   NA's   : 2.460e+02
> >  NA's   : 4.760e+02
> > FiscalQuarterRep    F12MRet
> > 2004/2F:  482    Min.   :-100.00
> > 2003/4F:  471    1st Qu.:  -8.82
> > 2004/1F:  470    Median :  10.57
> > 2004/3F:  470    Mean   :  13.36
> > 2003/3F:  464    3rd Qu.:  31.12
> > 2003/2F:  463    Max.   :4700.00
> > (Other):27284    NA's   : 463.00
> >> mode(Price)
> > [1] "numeric"
> >> mode(EPS)
> > [1] "numeric"
> >> mode(BookValuePS)
> > [1] "numeric"
> >> BP <- BookValuePS / Price ;
> > Warning message:
> > / not meaningful for factors in: Ops.factor(BookValuePS, Price)
> >> which(Price==0)
> > numeric(0)
>
> --
> Brian D. Ripley,                  [hidden email]
> 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
>


--
-- Vivek Satsangi
Student, Rochester, NY USA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

vincent.deluard
In reply to this post by Vivek Satsangi
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!
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

David Winsemius

On May 3, 2010, at 6:22 PM, vincent.deluard wrote:

>
> 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.

Not a particularly precise description of what is in the data.


> Every time I want to compute basic operations, R returns "In  
> Ops.factor: not
> meaningful for factors"

Code .... we want to see code. All of it.
>
> I believe it is because R does not read the data as numbers

Probably true. Were they dates or numbers?

> but I am not
> sure. Can anybody help?

I generally read in my data  with read.table( ,,,,, as.is=TRUE,  
stringsAsFactors=FALSE, ....) and then convert the columns that I know  
should be numeric with as.numeric. If all of your columns should have  
been numeric, you might use the colClasses argument, perhaps along  
these lines:

rd.obj <- read.csv(file="something", colClasses= rep("numeric",  
<number-of-columns>) )

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

John Kane-2
In reply to this post by vincent.deluard
 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.

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 <[hidden email]> wrote:

> From: vincent.deluard <[hidden email]>
> Subject: Re: [R] / Operator not meaningful for factors
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email]
> 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

RE: / Operator not meaningful for factors

vincent.deluard
In reply to this post by David Winsemius

Hi David,

 

Thanks for the prompt answer.

In this file, I have cut the data to the first 15 columns.

Here is the code:

 

t2 <- read.csv('Test2.csv', sep=",", header=TRUE)

> Test2[2,2]

[1] 18.72

1994 Levels: 0.24 0.33 0.49 0.53 0.65 0.67 0.72 0.76 0.88 0.9 0.94 ... 98.88

> Test2[2,2]+1

[1] NA

Warning message:

In Ops.factor(Test2[2, 2], 1) : + not meaningful for factors

 

The funny thing is that the same commands work normally for a shorter matrix – just the 1st 6 rows and columns.

Which led me to think it had to do with the blank cells.

But typing :

Test2 <- read.csv('Test2.csv', sep=",", header=TRUE, na.strings=" ")

does not help.

 

Your help is infinitely appreciated.

 

--------------------------------------------

Vincent Deluard

[hidden email]

Global Equity Strategist, CFA Charter Award Pending

TrimTabs Investment Research

40 Wall Street, 28th Floor

New York, NY 10005

Phone: (+1) 646-512-5616

 

From: David Winsemius [via R] [mailto:[hidden email]]
Sent: Monday, May 03, 2010 6:35 PM
To: vincent.deluard
Subject: Re: / Operator not meaningful for factors

 


On May 3, 2010, at 6:22 PM, vincent.deluard wrote:


>
> 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.


Not a particularly precise description of what is in the data.


> Every time I want to compute basic operations, R returns "In  
> Ops.factor: not
> meaningful for factors"

Code .... we want to see code. All of it.
>
> I believe it is because R does not read the data as numbers

Probably true. Were they dates or numbers?

> but I am not
> sure. Can anybody help?

I generally read in my data  with read.table( ,,,,, as.is=TRUE,  
stringsAsFactors=FALSE, ....) and then convert the columns that I know  
should be numeric with as.numeric. If all of your columns should have  
been numeric, you might use the colClasses argument, perhaps along  
these lines:

rd.obj <- read.csv(file="something", colClasses= rep("numeric",  
<number-of-columns>) )

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.


View message @ http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124710.html
To unsubscribe from Re: / Operator not meaningful for factors, click here.

 


Test2.csv (242K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: / Operator not meaningful for factors

vincent.deluard
In reply to this post by John Kane-2

Hi John,

 

Thanks for responding so quickly.

 

So I tried:

 

Test2[1:3,1:3])

'data.frame':   3 obs. of  3 variables:

 $ Date      : Factor w/ 2480 levels "A UN Equity",..: 1033 2396 613

 $ X4.20.2010: Factor w/ 1994 levels "0.24","0.33",..: 953 497 1814

 $ X4.19.2010: Factor w/ 1957 levels "0.24","0.33",..: 933 486 1779

 

But I am not sure how to interpret it.

 

The stringAsFactors = False option did not work either.

 

> Test2 <- read.table('Test2.csv', sep=",", header=TRUE, stringAsFactors=FALSE)

Error in read.table("Test2.csv", sep = ",", header = TRUE, stringAsFactors = FALSE) :

  unused argument(s) (stringAsFactors = FALSE)

 

If you want to look at it, I attach a section of my data.

 

Right now if I type:

> Test2[2,2]+1

Which should return 19.72

 

I get

[1] NA

Warning message:

In Ops.factor(Test2[2, 2], 1) : + not meaningful for factors

 

Your help is SO appreciated!

 

--------------------------------------------

Vincent Deluard

[hidden email]

Global Equity Strategist, CFA Charter Award Pending

TrimTabs Investment Research

40 Wall Street, 28th Floor

New York, NY 10005

Phone: (+1) 646-512-5616

 

From: John Kane-2 [via R] [mailto:[hidden email]]
Sent: Monday, May 03, 2010 6:51 PM
To: vincent.deluard
Subject: Re: / Operator not meaningful for factors

 

 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.

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 <[hidden email]> wrote:


> From: vincent.deluard <[hidden email]>
> Subject: Re: [R] / Operator not meaningful for factors
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email]
> 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.
>


______________________________________________
[hidden email] 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.


View message @ http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124731.html
To unsubscribe from Re: / Operator not meaningful for factors, click here.

 


Test2.csv (242K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

David Winsemius

On May 3, 2010, at 7:21 PM, vincent.deluard wrote:

>
> Hi John,
>
> Thanks for responding so quickly.
>
> So I tried:
>
> Test2[1:3,1:3])
>
> 'data.frame':   3 obs. of  3 variables:
>
> $ Date      : Factor w/ 2480 levels "A UN Equity",..: 1033 2396 613
>
> $ X4.20.2010: Factor w/ 1994 levels "0.24","0.33",..: 953 497 1814
>
> $ X4.19.2010: Factor w/ 1957 levels "0.24","0.33",..: 933 486 1779
>
>
>
> But I am not sure how to interpret it.

The information is now in the levels vector.

>
>
>
> The stringAsFactors = False option did not work either.

False <> FALSE

--  
David.


>
>
>
>> Test2 <- read.table('Test2.csv', sep=",", header=TRUE,  
>> stringAsFactors=FALSE)
>
> Error in read.table("Test2.csv", sep = ",", header = TRUE,  
> stringAsFactors = FALSE) :
>
>  unused argument(s) (stringAsFactors = FALSE)
>
>
>
> If you want to look at it, I attach a section of my data.
>
>
>
> Right now if I type:
>
>> Test2[2,2]+1
>
> Which should return 19.72
>

Not if it's a factor.

>
>
> I get
>
> [1] NA
>
> Warning message:
>
> In Ops.factor(Test2[2, 2], 1) : + not meaningful for factors
>
>
>
> Your help is SO appreciated!
>
>
>
> --------------------------------------------
>
> Vincent Deluard
>
> <mailto:[hidden email]> [hidden email]
>
> Global Equity Strategist, CFA Charter Award Pending
>
> TrimTabs Investment Research
>
> 40 Wall Street, 28th Floor
>
> New York, NY 10005
>
> Phone: (+1) 646-512-5616
>
>
>
> From: John Kane-2 [via R] [mailto:[hidden email]
> ]
> Sent: Monday, May 03, 2010 6:51 PM
> To: vincent.deluard
> Subject: Re: / Operator not meaningful for factors
>
>
>
> 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.
>
> 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 <[hidden email]> wrote:
>
>
>> From: vincent.deluard <[hidden email]>
>> Subject: Re: [R] / Operator not meaningful for factors
>> To: [hidden email]
>> 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.
>>
>> ______________________________________________
>> [hidden email]
>> 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.
>>
>
>
> ______________________________________________
> [hidden email] 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.
>
>
>
>  _____
>
> View message @ http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124731.html
> To unsubscribe from Re: / Operator not meaningful for factors, click
> < (link removed)
> AwNTU4Mzk3>  here.
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124759.html
> Sent from the R help mailing list archive at Nabble.com.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

jholtman
In reply to this post by John Kane-2
Check you input data.  You have some non-numeric characters in columns where
you are expecting numerics.

The parameter is

stringsAsFactors=FALSE

You had it spelt wrong.  After reading in your data, do the conversion to
numeric and then examine which locations contain NA; this will point you to
the problem line in your input.  Also if you use 'colClasses', I think it
will error the offending line.

On Mon, May 3, 2010 at 7:21 PM, vincent.deluard <
[hidden email]> wrote:

>
> Hi John,
>
>
>
> Thanks for responding so quickly.
>
>
>
> So I tried:
>
>
>
> Test2[1:3,1:3])
>
> 'data.frame':   3 obs. of  3 variables:
>
>  $ Date      : Factor w/ 2480 levels "A UN Equity",..: 1033 2396 613
>
>  $ X4.20.2010: Factor w/ 1994 levels "0.24","0.33",..: 953 497 1814
>
>  $ X4.19.2010: Factor w/ 1957 levels "0.24","0.33",..: 933 486 1779
>
>
>
> But I am not sure how to interpret it.
>
>
>
> The stringAsFactors = False option did not work either.
>
>
>
> > Test2 <- read.table('Test2.csv', sep=",", header=TRUE,
> stringAsFactors=FALSE)
>
> Error in read.table("Test2.csv", sep = ",", header = TRUE, stringAsFactors
> = FALSE) :
>
>  unused argument(s) (stringAsFactors = FALSE)
>
>
>
> If you want to look at it, I attach a section of my data.
>
>
>
> Right now if I type:
>
> > Test2[2,2]+1
>
> Which should return 19.72
>
>
>
> I get
>
> [1] NA
>
> Warning message:
>
> In Ops.factor(Test2[2, 2], 1) : + not meaningful for factors
>
>
>
> Your help is SO appreciated!
>
>
>
> --------------------------------------------
>
> Vincent Deluard
>
>  <mailto:[hidden email]> [hidden email]
>
> Global Equity Strategist, CFA Charter Award Pending
>
> TrimTabs Investment Research
>
> 40 Wall Street, 28th Floor
>
> New York, NY 10005
>
> Phone: (+1) 646-512-5616
>
>
>
> From: John Kane-2 [via R] [mailto:
> [hidden email]<ml-node%[hidden email]>
> ]
> Sent: Monday, May 03, 2010 6:51 PM
> To: vincent.deluard
> Subject: Re: / Operator not meaningful for factors
>
>
>
>  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.
>
> 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 <[hidden email]> wrote:
>
>
> > From: vincent.deluard <[hidden email]>
> > Subject: Re: [R] / Operator not meaningful for factors
> > To: [hidden email]
> > 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.
> >
> > ______________________________________________
> > [hidden email]
> > mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained,
> > reproducible code.
> >
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>  _____
>
> View message @
> http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124731.html
> To unsubscribe from Re: / Operator not meaningful for factors, click
> < (link removed)
> AwNTU4Mzk3>  here.
>
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Operator-not-meaningful-for-factors-tp791563p2124759.html
> Sent from the R help mailing list archive at Nabble.com.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<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?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

Erik Iverson-3
In reply to this post by David Winsemius

>>
>> The stringAsFactors = False option did not work either.
>
> False <> FALSE
>
>

in addition Vincent was getting an error since he used stringAsFactors, instead
of stringsAsFactors as the argument name.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

Don MacQueen
In reply to this post by John Kane-2
At 3:50 PM -0700 5/3/10, John Kane wrote:
>  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.
>
>

Well, "annoying" is in the eye of the beholder. The reason is not
weird at all; the original S language, upon which R is based, was
designed first for statistical analysis. When the language was
expanded to include advanced modeling capabilities (linear models,
generalized linear models, and more) it became apparent that factors
are the appropriate form for using categorical data in such models.
it is still the "R Project for Statistical Computing" (see the R home
page), so the default is unchanged.

Hence, when users get factors when they were expecting numbers, it's
virtually always because the have some non-numeric character strings
mixed in with the data. R then defaults to interpreting it as
categorical data, represented as a factor.

-Don
--
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

PIKAL Petr
In reply to this post by John Kane-2
Hi

[hidden email] 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 <[hidden email]>
wrote:

>
> > From: vincent.deluard <[hidden email]>
> > Subject: Re: [R] / Operator not meaningful for factors
> > To: [hidden email]
> > 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.
> >
> > ______________________________________________
> > [hidden email]
> > 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

PIKAL Petr
Hi

"Katya Mauff" <[hidden email]> 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 <[hidden email]> 2010/05/04 09:38 AM >>>
> Hi
>
> [hidden email] 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 <[hidden email]>
> wrote:
> >
> > > From: vincent.deluard <[hidden email]>
> > > Subject: Re: [R] / Operator not meaningful for factors
> > > To: [hidden email]
> > > 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.
> > >
> > > ______________________________________________
> > > [hidden email]
> > > 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.
> > >
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

PIKAL Petr
Hi

Yes, it is also possible. I usually use

as.numeric(as.character(some.factor.which can.be.transformed.to.numeric))

Regards
Petr

"Katya Mauff" <[hidden email]> napsal dne 04.05.2010 10:42:20:

> apologies-try something like this:
> > x<-rnorm(5)
> > x
> [1]  0.9128818  1.5615704 -1.2319878 -1.8689400 -1.2931785
> > x.f<-factor(x)
> > x.f
> [1] 0.912881849717846 1.56157044807315  -1.2319877852522
-1.8689399737313
> -1.29317849983031
> Levels: -1.8689399737313 -1.29317849983031 -1.2319877852522
0.912881849717846

> 1.56157044807315
> > y=as.numeric(levels(x.f))[x.f]
> > y
> [1]  0.9128818  1.5615704 -1.2319878 -1.8689400 -1.2931785
>
>
>
>
>
> >>> Petr PIKAL <[hidden email]> 2010/05/04 10:12 AM >>>
>
> > 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
>
>
______________________________________________________________________________________________
> 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: / Operator not meaningful for factors

John Kane-2
In reply to this post by PIKAL Petr


--- On Tue, 5/4/10, Petr PIKAL <[hidden email]> wrote:

> From: Petr PIKAL <[hidden email]>
> Subject: Re: [R] / Operator not meaningful for factors
> To: "John Kane" <[hidden email]>
> Cc: [hidden email], "vincent.deluard" <[hidden email]>
> Received: Tuesday, May 4, 2010, 3:38 AM
> Hi
>
> [hidden email]
> 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.

I'm not denying that it can be useful but IIRC from a discussion a couple of years ago, it was a fairly arbitary decision.

On the other hand it can be very annoying when one has some kinds of data.


>
> 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 <[hidden email]>
>
> wrote:
> >
> > > From: vincent.deluard <[hidden email]>
> > > Subject: Re: [R] / Operator not meaningful for
> factors
> > > To: [hidden email]
> > > 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.
> > >
> > > ______________________________________________
> > > [hidden email]
> > > 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.
> > >
> >
> > ______________________________________________
> > [hidden email]
> 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.
>
>



______________________________________________
[hidden email] 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.