简体   繁体   中英

How to remove vector with repeating elements from R data.frame?

dat = data.frame(do.call("rbind", list(c(1.5, 1.5, 1.5, 1.5, 1.5), c(1, 2, 3, 4, 5),
                           c(3, 3, 3, 3, 3), c(1, 2, 2, 3, 4))))

dat
   X1  X2  X3  X4  X5
1 1.5 1.5 1.5 1.5 1.5
2 1.0 2.0 3.0 4.0 5.0
3 3.0 3.0 3.0 3.0 3.0
4 1.0 2.0 2.0 3.0 4.0

Here I'd like to remove rows 1 and 3 because the elements in those rows are the same. I want the resulting data.frame to just consist of rows 2 and 4. What's a quick way to do this without writing a loop?

You can use apply + length + unique along with regular subsetting:

dat[apply(dat, 1, function(x) length(unique(x))) > 1, ]
#   X1 X2 X3 X4 X5
# 2  1  2  3  4  5
# 4  1  2  2  3  4

Here's what has been done:

  • apply(dat, 1, function(x) length(unique(x))) goes through the data.frame by row and returns how many unique values there are in each row.
  • > 1 creates a logical vector answering whether the row has more than one unique value or not.
  • We use that to subset the rows we want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM