[R] modify the levels of a variable

Henrique Dallazuanna wwwhsd at gmail.com
Thu Mar 11 01:52:30 CET 2010


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 <cheelee7 at gmail.com> 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.
>
> ______________________________________________
> 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.
>



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



More information about the R-help mailing list