使用 dplyr 有条件地将一列中的值替换为另一列中的值
Conditionally replace values in one column with values from another column using dplyr
我想将一列中与特定条件匹配的值替换为同一行中另一列中的值。考虑这个例子:
library(tidyverse)
data <- tribble(
~X25, ~Other,
"a", NA,
"b", NA,
"Other", "c",
"Other", "d"
)
View(data)
# Works to change values in X25
within(data, {
X25 <- ifelse(X25 == "Other", Other, X25)
})
# Changes values in X25 to NA and doesn't replace X25 with appropriate value from Other column
data %>% mutate(X25 = replace(X25, X25 == "Other", Other))
使用"within"的代码运行良好。如果需要,我如何使用 dplyr(作为更长的变异/总结过程的一部分)?
编辑:这是与 不同的场景。我不想盲目地为所有匹配的单元格分配相同的值(例如,NA)。我想从另一个特定的专栏中提取它们。
与replace
的长度应该相同,所以我们需要用逻辑表达式
对Other
进行子集化
data %>%
mutate(X25 = replace(X25, X25 == "Other", Other[X25=="Other"]))
另一种选择是 case_when
data %>%
mutate(X25 = case_when(X25=="Other"~ Other,
TRUE ~ X25))
或ifelse
data %>%
mutate(X25 = ifelse(X25 == "Other", Other, X25))
我想将一列中与特定条件匹配的值替换为同一行中另一列中的值。考虑这个例子:
library(tidyverse)
data <- tribble(
~X25, ~Other,
"a", NA,
"b", NA,
"Other", "c",
"Other", "d"
)
View(data)
# Works to change values in X25
within(data, {
X25 <- ifelse(X25 == "Other", Other, X25)
})
# Changes values in X25 to NA and doesn't replace X25 with appropriate value from Other column
data %>% mutate(X25 = replace(X25, X25 == "Other", Other))
使用"within"的代码运行良好。如果需要,我如何使用 dplyr(作为更长的变异/总结过程的一部分)?
编辑:这是与
与replace
的长度应该相同,所以我们需要用逻辑表达式
Other
进行子集化
data %>%
mutate(X25 = replace(X25, X25 == "Other", Other[X25=="Other"]))
另一种选择是 case_when
data %>%
mutate(X25 = case_when(X25=="Other"~ Other,
TRUE ~ X25))
或ifelse
data %>%
mutate(X25 = ifelse(X25 == "Other", Other, X25))