我有一个包含大量列的数据框。有些列以_id结尾。例如,food_id,drink_id。我想转换以" _id"结尾的所有列中的数据从因素到数字。我怎样才能在dplyr中这样做呢?
答案 0 :(得分:3)
我们可以使用mutate_at
library(dplyr)
library(magrittr)
df1 %<>%
mutate_at(vars(ends_with("_id")), funs(as.numeric(as.character(.))))
如果我们想要选择特定的&#39; id&#39;列
df2 %<>%
mutate_at(vars(matches("^(food|drink).*_id")), funs(as.numeric(as.character(.))))
或反向逻辑
df2 %<>%
mutate_at(vars(ends_with("_id"), -matches("^(snack|dessert).*_id")),
funs(as.numeric(as.character(.))))
或者在使用grep
之前创建列索引,然后在mutate_at
中使用
i1 <- !grepl("^(snack|dessert).*_id$", names(df2)) & grepl("_id$", names(df2))
df2 %<>%
mutate_at(vars(which(i1)), funs(as.numeric(as.character(.))))
set.seed(24)
df1 <- data.frame(food_id = factor(1:5), drink_id = factor(6:10), value = rnorm(5))
df2 <- data.frame(food_id = factor(1:5), drink_id = factor(6:10), value = LETTERS[1:5],
snack_id = factor(1:5), dessert_id = factor(11:15))