Vl_dem
Vl_dem

Reputation: 45

Detect rows which have only one number in a column

I face a problem where my dataframe

data.frame(mytest = c(0.43,0,0), additional = c(0,323,41.2), col = c(0,23.1,0.324))

Is it possible to detect which rows have only one number and remove them totally from the data.frame? results after removal:

data.frame(mytest = c(0,0), additional = c(323,41.2), col = c(23.1,0.324))

Upvotes: 0

Views: 66

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

I think the rowSums answer from @Duck would work the best but here is an alternative with row-wise apply :

df[apply(df != 0, 1, sum) > 1, ]

#  mytest additional    col
#2      0      323.0 23.100
#3      0       41.2  0.324

We select rows where number of non-zero values in a row is greater than 1.

Upvotes: 1

Related Questions