I have some data that looks like this:
List_name Condition1 Condition2 Situation1 Situation2 List1 0.01 0.12 66 123 List2 0.23 0.22 45 -34 List3 0.32 0.23 13 -12 List4 0.03 0.56 -3 45 List5 0.56 0.05 12 100 List6 0.90 0.09 22 32
I would like to filter each column “Condition” of the data.frame according to a cut off 0.5.
After the filter, the subset will occur and will carry the corresponding value of columns “Situation”. Filter and subset will work pairwise: “Condition1” with “Situation1”, “Condition2” with “Situation2” and so on.
Just the desired output:
List_name Condition1 Situation1 List_name Condition2 Situation2 List1 0.01 66 List1 0.12 123 List2 0.23 45 List2 0.22 -34 List3 0.32 13 List3 0.23 -12 List4 0.03 -3 List5 0.05 100 List6 0.09 32
I’m pretty sure that there’s probably another similar situation posted before but I searched and I didn’t find it.
You can use the notion that boolean checks are vectorized:
And some
grepresults:To do this subsetting you can use
applyto generate your boolean vector:And subset:
Notice that this doesn’t necessarily return the data structure you showed in your question. But you can alter the anonymous function accordingly using
allor a different threshold value.In lieu of the edits, I would approach this differently. I would use
meltfrom thereshape2package:You can then use
dcastto put the data back in the initial format if you want, but I find data in this “long” form much easier to work with. This form is also pleasant since it avoids the need for NA values where you have rows where one condition is met and others are not.