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)?

4

1 回答 1

3

尝试:

df[rowSums(df==1) == 1,]
     var1 var2 var3
[1,]    0    0    1
[2,]    1    0    0
于 2014-04-01T18:34:25.853 回答