[英]How to find column with first of value in R using dplyr?
Consider the following example data:考虑以下示例数据:
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
I want to find the column name with the first 0 for each row.我想为每行找到第一个 0 的列名。 If there are no zeros, then I want to get the last column name.如果没有零,那么我想获取最后一个列名。
The result should look like this:结果应如下所示:
# 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
from base R
is fast base R
max.col
max.col 很快
d$firstZero <- ifelse(!rowSums(d == 0), names(d)[ncol(d)],
names(d)[max.col(d == 0, 'first')])
using dplyr
使用dplyr
library(dplyr)
d %>%
rowwise %>%
mutate(firstZero = names(.)[match(0, c_across(everything()),
nomatch = ncol(.))]) %>%
ungroup
-output -输出
# 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
Or using case_when
with max.col
或者将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"))])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.