Search code examples
rvectorr-factor

R: Make new vector correlating to a specified level in a factor


I'm stuck with a really easy task and hope someone could help me with it..

I'd like to make a new (sub)vector from an existing vector, based on 1 level of a factor. Example:

v = c(1,2,3,4,5,6,7,8,9,10)
f = factor(rep(c("Drug","Placebo"),5))

I want to make a new vector from v, containing only "Drug" or "Placebo". Resulting in:

vDrug = 1,3,5,7,9   
vPlacebo = 2,4,6,8,10

Thanks in advance!


Solution

  • You can easily subset v by f:

    v[ f == "Drug" ]
    [1] 1 3 5 7 9
    

    However, this approach might become error prone in a more complex environment or with larger data sets. Accordingly it would be better to store vand f together in a data.frame and than perform on this data.frame all kinds of queries and transformations:

    mdf <- data.frame( v = c(1,2,3,4,5,6,7,8,9,10), f = factor(rep(c("Drug","Placebo"),5)) )
    mdf
        v       f
    1   1    Drug
    2   2 Placebo
    3   3    Drug
    4   4 Placebo
    ...
    

    If you want to look at your data interactively, you can subset using the subset function:

    subset( mdf, f == "Drug", select=v )
    

    If you are doing this programmatically, you should rather use

    mdf[ mdf$f == "Drug", "v" ]
    

    For the difference of the two have a look at: Why is `[` better than `subset`?.