如何根据某个行值重命名分组 dplyr tibble 中的列?下图显示了我的 tibble 之前的外观以及操作后的外观。
我尝试了以下代码,但没有设法编写能够从“名称”列灵活声明新列名称的列重命名函数。
library(dplyr)
df <- data.frame(
"splitvar"=c(1,1,1,2,2,3,3,3,3),
"value"=c(1,4,2,5,6,9,11,13,12),
"name"=c("Harold","Harold","Harold","Jane","Jane","George","George","George","George"),
stringsAsFactors=F
)
grouped_tbl <- df %>%
group_by( splitvar ) %>%
eval(parse(
paste0("rename(",unique(name)," = value)")
))