如何使用 dplyr 在 R 中查找具有第一个值的列?
How to find column with first of value in R using dplyr?
考虑以下示例数据:
d <- tibble(V1 = c(5, 3, 8, 7),
V2 = c(4, 0, 2, 6),
V3 = c(0, 1, 0, 3),
V4 = c(0, 0, 0, 2))
# A tibble: 4 × 4
V1 V2 V3 V4
<dbl> <dbl> <dbl> <dbl>
1 5 4 0 0
2 3 0 1 0
3 8 2 0 0
4 7 6 3 2
我想为每行找到第一个 0 的列名。如果没有零,那么我想获取最后一个列名。
结果应如下所示:
# A tibble: 4 × 5
V1 V2 V3 V4 firstZero
<dbl> <dbl> <dbl> <dbl> <chr>
1 5 4 0 0 V3
2 3 0 1 0 V2
3 8 2 0 0 V3
4 7 6 3 2 V4
max.col
来自 base R
很快
d$firstZero <- ifelse(!rowSums(d == 0), names(d)[ncol(d)],
names(d)[max.col(d == 0, 'first')])
使用dplyr
library(dplyr)
d %>%
rowwise %>%
mutate(firstZero = names(.)[match(0, c_across(everything()),
nomatch = ncol(.))]) %>%
ungroup
-输出
# A tibble: 4 × 5
V1 V2 V3 V4 firstZero
<dbl> <dbl> <dbl> <dbl> <chr>
1 5 4 0 0 V3
2 3 0 1 0 V2
3 8 2 0 0 V3
4 7 6 3 2 V4
或使用 case_when
和 max.col
d %>%
mutate(firstZero = names(.)[case_when(!rowSums(across(c(V1, V2, V3)) == 0) ~
ncol(.), TRUE ~ max.col(!across(c(V1, V2, V3)) != 0, "first"))])
考虑以下示例数据:
d <- tibble(V1 = c(5, 3, 8, 7),
V2 = c(4, 0, 2, 6),
V3 = c(0, 1, 0, 3),
V4 = c(0, 0, 0, 2))
# A tibble: 4 × 4
V1 V2 V3 V4
<dbl> <dbl> <dbl> <dbl>
1 5 4 0 0
2 3 0 1 0
3 8 2 0 0
4 7 6 3 2
我想为每行找到第一个 0 的列名。如果没有零,那么我想获取最后一个列名。
结果应如下所示:
# A tibble: 4 × 5
V1 V2 V3 V4 firstZero
<dbl> <dbl> <dbl> <dbl> <chr>
1 5 4 0 0 V3
2 3 0 1 0 V2
3 8 2 0 0 V3
4 7 6 3 2 V4
max.col
来自 base R
很快
d$firstZero <- ifelse(!rowSums(d == 0), names(d)[ncol(d)],
names(d)[max.col(d == 0, 'first')])
使用dplyr
library(dplyr)
d %>%
rowwise %>%
mutate(firstZero = names(.)[match(0, c_across(everything()),
nomatch = ncol(.))]) %>%
ungroup
-输出
# A tibble: 4 × 5
V1 V2 V3 V4 firstZero
<dbl> <dbl> <dbl> <dbl> <chr>
1 5 4 0 0 V3
2 3 0 1 0 V2
3 8 2 0 0 V3
4 7 6 3 2 V4
或使用 case_when
和 max.col
d %>%
mutate(firstZero = names(.)[case_when(!rowSums(across(c(V1, V2, V3)) == 0) ~
ncol(.), TRUE ~ max.col(!across(c(V1, V2, V3)) != 0, "first"))])