[英]Remove NA elements from column of vectors using dplyr
I am trying to merge columns into one column, I've combined them using pmap
.我正在尝试将列合并为一列,我已使用
pmap
将它们合并。
Some columns have NAs, I'd like to remove the NAs from the combined column col_comb
and leave the other values.有些列有 NA,我想从组合列
col_comb
中删除 NA 并保留其他值。
df = tribble(~id, ~col1, ~col2, ~col3,
1, "a", "b", "c",
2, "a", NA, "c",
3, "a", NA, NA,
4, NA, NA, NA)
df = df %>%
mutate(col_comb = pmap(list(col1,col2,col3), c))
We can specify a lambda function to do this我们可以指定一个 lambda function 来做到这一点
library(dplyr)
library(purrr)
df1 <- df %>%
mutate(col_comb = pmap(select(., starts_with('col')),
~ as.character(na.omit(c(...)))))
-output -输出
df1$col_comb
#[[1]]
#[1] "a" "b" "c"
#[[2]]
#[1] "a" "c"
#[[3]]
#[1] "a"
#[[4]]
#character(0)
Or another option is c_across
with rowwise
或者另一个选项是
c_across
与rowwise
df %>%
rowwise %>%
mutate(col_comb = list(na.omit(c_across(-id)))) %>%
ungroup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.