[R] Reordering levels of a factor when the factor is part of a data frame

2012-02-06 Thread Judith Flores
Hello R-users,

   I have a data frame whose names of columns I don't know a priori, but the 
user of my code will know them. The user is supposed to save the name of the 
column that will need some reordering of the levels of the factor later on. The 
name of the column will be saved in an object called:

variab

the data frame is called df.


If I try to the do following:

df[variab]-factor(df[variab], levels=c(A2B,B31,C33))

it won't work because df[variab] is a data frame. The reason for reordering the 
levels of the factor is because once that variable is plotted, the levels of 
the factor need to appear in certain order.

   How can I re-order the levels of a factor whose name I don't know?


Thank you,

Judith
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Reordering levels of a factor when the factor is part of a data frame

2012-02-06 Thread Ista Zahn
Hi Judith,

You should use double brackets, like this:

df[[variab]]-factor(df[[variab]], levels=c(A2B,B31,C33))

see ?[ for details, noting that the help page assumes that you know
data.frames are list-like objects.

Best,
Ista

On Mon, Feb 6, 2012 at 2:03 PM, Judith Flores jur...@yahoo.com wrote:
 Hello R-users,

    I have a data frame whose names of columns I don't know a priori, but the 
 user of my code will know them. The user is supposed to save the name of the 
 column that will need some reordering of the levels of the factor later on. 
 The name of the column will be saved in an object called:

 variab

 the data frame is called df.


 If I try to the do following:

 df[variab]-factor(df[variab], levels=c(A2B,B31,C33))

 it won't work because df[variab] is a data frame. The reason for reordering 
 the levels of the factor is because once that variable is plotted, the levels 
 of the factor need to appear in certain order.

    How can I re-order the levels of a factor whose name I don't know?


 Thank you,

 Judith
        [[alternative HTML version deleted]]


 __
 R-help@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@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.


Re: [R] Reordering levels of a factor when the factor is part of a data frame

2012-02-06 Thread Ernest Adrogué
 6-02-2012, 11:03 (-0800); Judith Flores escriu:
 The name of the column will be saved in an object called:
 
 variab
 
 the data frame is called df.
 
 
 If I try to the do following:
 
 df[variab]-factor(df[variab], levels=c(A2B,B31,C33))
 
 it won't work because df[variab] is a data frame. 

I think you need to use df[[variab]] instead.

Cheers,
Ernest

__
R-help@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.