[R] subset data right

William Dunlap wdunlap at tibco.com
Thu May 26 23:48:24 CEST 2016


You did not change df$quant - you made a new object called 'subdf'
containing a column called 'quant' that had only one level.  Changing
subdf has no effect on df.

> df <- data.frame(quant=factor(letters))
> str(df)
'data.frame':   26 obs. of  1 variable:
 $ quant: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
> subdf <- subset(df, quant %in% "a")
> subdf$quant <- factor(subdf$quant)
> str(df)
'data.frame':   26 obs. of  1 variable:
 $ quant: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
> str(subdf)
'data.frame':   1 obs. of  1 variable:
 $ quant: Factor w/ 1 level "a": 1



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, May 26, 2016 at 12:35 PM, ch.elahe via R-help <r-help at r-project.org>
wrote:

> Hi all,
> I have the following df and I want to know which Protocols are VeryFast,
> which are FAST, which are SLOW and also which ones are VerySLOW :
>
>
>   $ Protocol       : Factor w/ 48 levels "DP FS QTSE SAG",..: 5 5 28 5 5 5
> 7 7 47 5 ...
>
>   $ quant          : Factor w/ 4 levels "FAST","SLOW",..: 2 2 2 4 2 1 1 2 4
>
> I do the following subset but nothing is changed in my df:
>
>
>   subdf=subset(df,quant%in%c("VeryFast"))
>   subdf$quant=factor(subdf$quant)
> and when I get the str(df) again Protocol has 48 levels. Does anyone know
> how can I get these subsets right?
> Thanks for any help!
> Elahe
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list