如何识别每一行的最大值(例如:b1和a4中的20)是同一行的下一个最大值(b1和a3中的5个)的2倍?
示例输入
name a1 a2 a3 a4
b1 1 1 5 20
b2 10 9 20 9
b3 1 1 1 1
示例输出
b1 a4 20
b2 a3 20
答案 0 :(得分:1)
使用:
library(dplyr)
library(tidyr)
d %>%
gather(k, v, -name) %>%
arrange(-v, name) %>%
group_by(name) %>%
mutate(logi = v / lead(v, default = min(v)) >= 2) %>%
filter(v == first(v) & logi) %>%
select(-logi)
结果是:
# A tibble: 2 x 3
# Groups: name [2]
name k v
<fctr> <chr> <int>
1 b1 a4 20
2 b2 a3 20
使用的数据:
d <- structure(list(name = structure(1:3, .Label = c("b1", "b2", "b3"), class = "factor"),
a1 = c(1L, 10L, 1L),
a2 = c(1L, 9L, 1L),
a3 = c(5L, 20L, 1L),
a4 = c(20L, 9L, 1L)),
.Names = c("name", "a1", "a2", "a3", "a4"), class = "data.frame", row.names = c(NA, -3L))