I am interning in a computer science lab and I'm very new to R. The language basics are clear, but this particular problem isn't: I have a very large dataframe called "data" which holds data from Halo matches. I'm trying to analyze a certain window such that data$deaths>20 and data$deaths<=27. When I enter the line kills = data$kills[data$deaths>20] or any single condition, it selects the region I want. However, when I input kills = data$kills[data$deaths>20 && data$deaths<=27] "kills" becomes the entire vector data$kills, without selecting any specific region. This is a simple problem, but I can't find anything wrong. How could I fix it? -- View this message in context: http://r.789695.n4.nabble.com/Basic-vector-logic-not-working-tp3656465p3656465.html Sent from the R help mailing list archive at Nabble.com.
DimmestLemming wrote:> > I am interning in a computer science lab and I'm very new to R. The > language basics are clear, but this particular problem isn't: > > I have a very large dataframe called "data" which holds data from Halo > matches. I'm trying to analyze a certain window such that data$deaths>20 > and data$deaths<=27. > > When I enter the line > > kills = data$kills[data$deaths>20] > > or any single condition, it selects the region I want. However, when I > input > > kills = data$kills[data$deaths>20 && data$deaths<=27] > > "kills" becomes the entire vector data$kills, without selecting any > specific region. > > This is a simple problem, but I can't find anything wrong. How could I fix > it? >Maybe something like ... # example data d = data.frame(age=1:20, names=letters[1:20]) # 1. subset using [ d[d$age>10 & d$age<16,] # 2. subset using subset function subset(d,d$age>10 & d$age<16) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Basic-vector-logic-not-working-tp3656465p3656594.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of DimmestLemming > Sent: Saturday, July 09, 2011 10:31 AM > To: r-help at r-project.org > Subject: [R] Basic vector logic not working > > I am interning in a computer science lab and I'm very new to R. The > language > basics are clear, but this particular problem isn't: > > I have a very large dataframe called "data" which holds data from Halo > matches. I'm trying to analyze a certain window such that data$deaths>20 > and > data$deaths<=27. > > When I enter the line > > kills = data$kills[data$deaths>20] > > or any single condition, it selects the region I want. However, when I > input > > kills = data$kills[data$deaths>20 && data$deaths<=27] > > "kills" becomes the entire vector data$kills, without selecting any > specific > region. > > This is a simple problem, but I can't find anything wrong. How could I fix > it? >You need to look at the difference between & and &&. & does position by position comparison, while && only considers the first element of each vector. For your purposes, I believe you should be using a single ampersand, &. Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
Thanks so much! The single & did it, and now I know how that works -- View this message in context: http://r.789695.n4.nabble.com/Basic-vector-logic-not-working-tp3656465p3657958.html Sent from the R help mailing list archive at Nabble.com.