Dplyr:重新编码数字和字符向量

时间:2019-10-25 09:05:05

标签: r dplyr recode

我心中有一个死亡的画面。如何在recode中使用dplyr

data = data.frame(id=rep(1:10, each=1), time=seq(1:5), char = strrep("a", 3))

library(dplyr) # everything throws an error
data %>% recode(time, `1` = 0)
data %>% recode(time, `1` = 0L)
data %>% recode(time, 1 = 0)
data %>% recode(time, 1 = 0L)

data %>% recode(char, aaa = 0)
data %>% recode(time, "aaa" = 0)

最诚挚的问候

1 个答案:

答案 0 :(得分:2)

这是因为recode将向量作为参数,而不是data.frame:

data %>% mutate(time2 = recode(time, `1` = 0L))
data %>% mutate(char2 = recode(char, aaa = 0))
data %>% mutate(char2 = recode(as.character(char), aaa = 0))