I often have to transform long tidy data frames into wide format. To do so I use the following standard procedure:
# Example data frame
df <- data.frame("ID" = rep(1:5, each = 4), "score" = runif(20, 0, 100), "location" = rep(c("a", "b", "c", "d"), 5))
# Transform into wide format
df_wide <- df %>%
group_by_at(vars(-score)) %>% # group by everything other than the value column.
mutate(row_id=1:n()) %>% ungroup() %>% # build group index
spread(key=location, value=score) %>% # spread
dplyr::select(-row_id)
Instead of typing this little script over and over again, I wanted to define a function to do it automatically. I found many useful posts on how to include column names as function inputs, but somehow it doesn't work or I get error messages. What am I doing wrong?
Below a few of my attempts (neither of them work), following these and this suggestions:
wide_fun <- function(dat, key_name, value_name) {
group_by_at(vars(- !! sym(value_name))) %>% # group by everything other than the value column.
mutate(row_id=1:n()) %>% ungroup() %>% # build group index
spread(key=!! sym(key_name), value=!! sym(value_name)) %>% # spread
dplyr::select(-row_id)
}
wide_fun2 <- function(dat, key_name, value_name) {
key_col <- enquo(key_name)
value_col <- enquo(value_name)
group_by_at(vars(- !!value_col)) %>% # group by everything other than the value column.
mutate(row_id=1:n()) %>% ungroup() %>% # build group index
spread(key= !!key_col, value= !!value_col) %>% # spread
dplyr::select(-row_id)
}
wide_fun3 <- function(dat, key_name, value_name) {
group_by_at(vars(- value_name)) %>% # group by everything other than the value column.
mutate(row_id=1:n()) %>% ungroup() %>% # build group index
spread(key=key_name, value=value_name) %>% # spread
dplyr::select(-row_id)
}
wide_fun3(df, quote(location), quote(score))
Thanks for your help!