How can I rename a column within a grouped dplyr tibble, depending on a certain row value? The following graphic shows how my tibble looks before and how it should be looking after the manipulation.
I have tried the following code, but did not manage to write a column rename function that is able to state the new column name flexibly from the "name" column.
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)")
))
Related: Replacement for "rename" in dplyr
Like this:
It took some time getting my head around the quosure-thing, but try and take a look at programming with dplyr
The output of the code is: