group_by table 如果列的总和在 R 中为 0 到 NA,则更改值
group_by table and change the values if the sum of the column is 0 to NA in R
我有一个 table 并将其按两列分组。在每个组中,我想更改几列。如果一列(组中)的总和为 0,则应将此列中的每一行更改为 NA。最后我想要开始 table,但是用 NA 而不是 0.
我试过了:
table_neu <- table %>%
group_by(A,B) %>%
mutate_at(.vars = vars(C_01:C_12),
.funs = funs(case_when(
sum(.) == 0 ~ NA,
TRUE ~ .)),
na.rm = TRUE) %>%
ungroup()
我收到此错误,但不知道是什么问题。
Case 3 (colsum(C_01) == 0 ~ NA
) must be a two-sided formula, not a logical vectorTraceback:
尝试使用:
library(dplyr)
table_neu <- table %>%
group_by(A,B) %>%
mutate_at(vars(C_01:C_12),~case_when(
sum(., na.rm = TRUE) == 0 ~ NA_real_,
TRUE ~ .)) %>%
ungroup()
我有一个 table 并将其按两列分组。在每个组中,我想更改几列。如果一列(组中)的总和为 0,则应将此列中的每一行更改为 NA。最后我想要开始 table,但是用 NA 而不是 0.
我试过了:
table_neu <- table %>%
group_by(A,B) %>%
mutate_at(.vars = vars(C_01:C_12),
.funs = funs(case_when(
sum(.) == 0 ~ NA,
TRUE ~ .)),
na.rm = TRUE) %>%
ungroup()
我收到此错误,但不知道是什么问题。
Case 3 (
colsum(C_01) == 0 ~ NA
) must be a two-sided formula, not a logical vectorTraceback:
尝试使用:
library(dplyr)
table_neu <- table %>%
group_by(A,B) %>%
mutate_at(vars(C_01:C_12),~case_when(
sum(., na.rm = TRUE) == 0 ~ NA_real_,
TRUE ~ .)) %>%
ungroup()