简体   繁体   English

R:使新向量与因子中的指定级别相关联

[英]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. 我想基于1个因子级别从现有向量中创建一个新的(子)向量。 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". 我想从v制作一个新的载体,只包含“药物”或“安慰剂”。 Resulting in: 导致:

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

Thanks in advance! 提前致谢!

You can easily subset v by f : 您可以通过f轻松地将v子集:

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 v and f together in a data.frame and than perform on this data.frame all kinds of queries and transformations: 因此,最好将vf存储在data.frame而不是对此data.frame执行各种查询和转换:

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函数进行subset化:

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`? 对于两者的区别,请看: 为什么`[`比`子集更好? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM