modify the levels of a variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

modify the levels of a variable

clee
If anyone can help with this, I would greatly appreciate it.

When I subset a dataframe, and then create a table of on of the column variables, the levels of that variable are retained from the parent dataframe.  Anyone know how to


for example, say I have data like so:

data:
X     Y
a     2
b     2
a     3
b     3
a     5


if I do....

data2=subset(data,X=="a")

I get the new data frame:

data2:
X     Y
a     2
a     3
a     5


but if I do this...

table(data2$X)

I would get this:

X     Y
3     0


additionally when I write...

levels(data2$X)

I get...
Factors w/ 2 levels: "a", "b"

I want it to be...
Factors w/ 1 level: "a"

Normally I would just move on and won't even bother but I'm working with a variable that has 204 levels and only 84 of those are meaningful to me.  How can I reset the levels of a column variable?  help please!  Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: modify the levels of a variable

milton ruser
Try

data2$X<-factor(data2$X)

By the way, avoid call data as "data", matrix as "matrix", vector as
"vector".

cheers

milton

On Wed, Mar 10, 2010 at 4:35 PM, clee <[hidden email]> wrote:

>
> If anyone can help with this, I would greatly appreciate it.
>
> When I subset a dataframe, and then create a table of on of the column
> variables, the levels of that variable are retained from the parent
> dataframe.  Anyone know how to
>
>
> for example, say I have data like so:
>
> data:
> X     Y
> a     2
> b     2
> a     3
> b     3
> a     5
>
> if I do....
>
> data2=subset(data,X=="a")
>
> I get the new data frame:
>
> data2:
> X     Y
> a     2
> a     3
> a     5
>
> but if I do this...
>
> table(data2$X)
>
> I would get this:
>
> X     Y
> 3     0
>
> additionally when I write...
>
> levels(data2$X)
>
> I get...
> Factors w/ 2 levels: "a", "b"
>
> I want it to be...
> Factors w/ 1 level: "a"
>
> Normally I would just move on and won't even bother but I'm working with a
> variable that has 204 levels and only 84 of those are meaningful to me.
>  How
> can I reset the levels of a column variable?  help please!  Thanks!
> --
> View this message in context:
> http://n4.nabble.com/modify-the-levels-of-a-variable-tp1588089p1588089.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.
>

        [[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: modify the levels of a variable

Henrique Dallazuanna
In reply to this post by clee
You can try this also:

as.data.frame(lapply(subset(data, X == 'a'), '[', drop = TRUE))

On Wed, Mar 10, 2010 at 6:35 PM, clee <[hidden email]> wrote:

>
> If anyone can help with this, I would greatly appreciate it.
>
> When I subset a dataframe, and then create a table of on of the column
> variables, the levels of that variable are retained from the parent
> dataframe.  Anyone know how to
>
>
> for example, say I have data like so:
>
> data:
> X     Y
> a     2
> b     2
> a     3
> b     3
> a     5
>
> if I do....
>
> data2=subset(data,X=="a")
>
> I get the new data frame:
>
> data2:
> X     Y
> a     2
> a     3
> a     5
>
> but if I do this...
>
> table(data2$X)
>
> I would get this:
>
> X     Y
> 3     0
>
> additionally when I write...
>
> levels(data2$X)
>
> I get...
> Factors w/ 2 levels: "a", "b"
>
> I want it to be...
> Factors w/ 1 level: "a"
>
> Normally I would just move on and won't even bother but I'm working with a
> variable that has 204 levels and only 84 of those are meaningful to me.  How
> can I reset the levels of a column variable?  help please!  Thanks!
> --
> View this message in context: http://n4.nabble.com/modify-the-levels-of-a-variable-tp1588089p1588089.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.
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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