【发布时间】:2018-10-18 16:09:38
【问题描述】:
我想用不同列中同一行中的值替换与特定条件匹配的列中的值。考虑这个例子:
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))
使用“内部”的代码运行良好。如果需要(作为更长的变异/汇总过程的一部分),我如何使用 dplyr?
编辑:这是与Change value of variable with dplyr 不同的场景。我不想盲目地为所有匹配的单元格分配相同的值(例如,NA)。我想把它们从另一个特定的列中拉出来。
【问题讨论】:
标签: r replace dplyr conditional