R: Make new vector correlating to a specified level in a factor -
I'm really stuck with a simple task and hope someone can help me ..
Based on the 1 level of a factor, I want to create a new vector from an existing vector. Example:
v = c (1,2,3,4, 5,6,7,8,9,10) f = factor (representative (C ("drug", "placebo"), 5))
I create a new vector from V I want, in which only "drug" or "placebo" is included. As a result of this:
vDrug = 1,3,5,7,9 vPlacebo = 2,4,6,8,10
Thanks in advance!
You can easily v
by f
Can reduce:
v [f == "drug"] [1] 1 3 5 7 9
However, in this approach more The error can be with complex environments or larger data sets accordingly v
and f
in data.frame
and data.frame < It would be better to do better than performing on / code>.
All types of questions and changes:
mdf & lt; - data.frame (v = c (1,2,3,4,5,6,7,8,9,10), F = factor (representative (c ("drug", "placebo"), 5)) ) MDF vf 1 1 drug 2 2 placebo 3 3 drug 4 4 placebo ...
If you want to see your data interactively, you can use subset
Function:
subset (MDF, F == "drug", select = V)
If you are doing this program, then You see for the difference between both
mdf [mdf $ f == "drug", "v"]
.
Comments
Post a Comment