How to select rows from data.frame with 2 conditions

Posted by Peter Smit on Stack Overflow See other posts from Stack Overflow or by Peter Smit
Published on 2009-10-08T09:07:24Z Indexed on 2010/06/16 12:02 UTC
Read the original article Hit count: 196

Filed under:

I have an aggregated table:

> aggdata[1:4,]
  Group.1 Group.2         x
1       4    0.05 0.9214660
2       6    0.05 0.9315789
3       8    0.05 0.9526316
4      10    0.05 0.9684211

How can I select the x value when I have values for Group.1 and Group.2?

I tried:

aggdata[aggdata[,"Group.1"]==l && aggdata[,"Group.2"]==lamda,"x"]

but that replies all x's.

More info: I want to use this like this:

table = data.frame();
for(l in unique(aggdata[,"Group.1"])) {
    for(lambda in unique(aggdata[,"Group.2"])) {
        table[l,lambda] = aggdata[aggdata[,"Group.1"]==l & aggdata[,"Group.2"]==lambda,"x"]
    }
}

Any suggestions that are even easier and giving this result I appreciate!

© Stack Overflow or respective owner

Related posts about r