0

I'm choosing observations to be included in a subset of a larger data set

R code:

var1 <- c(1,0,0,3,1)
var2 <- c(0,0,0,0,0)
var3 <- c(1,1,0,0,0)

df <- cbind(var1, var2, var3)

How could I select the subset of the data that contains only observations having one "1" in any given column (in this case I should end up selecting rows 2 and 5)?

Patrick Coulombe
  • 305
  • 1
  • 4
  • 18
HueSX
  • 145
  • 5

1 Answers1

3

Try:

df[rowSums(df==1) == 1,]
     var1 var2 var3
[1,]    0    0    1
[2,]    1    0    0
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Thank you so much hrbrmstr! May I ask a related question--using the same df, how could I isolate the subset that only has 1 non-zero cell in any row (so that I end up with the subset that has only rows 2,4,5)? – HueSX Apr 01 '14 at 19:01
  • If I read that correctly, then `df[rowSums(df!=0) == 1,]` should give you that. – hrbrmstr Apr 01 '14 at 19:10