简体   繁体   中英

R Data Frame remove rows with max values from all columns

Hello I have the data frame and I need to remove all the rows with max values from each columns. Example

   A B C
1  2 3 5
2  4 1 1
3  1 4 3
4  2 1 1

So the output is:

   A B C
4  2 1 1

Is there any quick way to do this?

We can do this with %in%

df1[!seq_len(nrow(df1)) %in% sapply(df1, which.max),]
#  A B C
#4 2 1 1

If there are ties for maximum values in each row, then do

df1[!Reduce(`|`, lapply(df1, function(x) x== max(x))),]
df[-sapply(df, which.max),]
#  A B C
#4 2 1 1

DATA

df = structure(list(A = c(2L, 4L, 1L, 2L), B = c(3L, 1L, 4L, 1L), 
    C = c(5L, 1L, 3L, 1L)), .Names = c("A", "B", "C"),
    class = "data.frame", row.names = c(NA,-4L))

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