I'm trying to get a value in a column to set as the column name. The characters that come before a colon should be the column name.
df = cbind.data.frame(
id = c(1, 2 ,3, 4, 5),
characteristics_ch1 = c("gender: Female", "gender: Male", "gender: Female", "gender: Male", "gender: Female"),
characteristics_ch1.1 = c("Thing One: a", "Thing One: a", "Thing One: a", "Thing One: b", "Thing One: b"),
characteristics_ch1.2 = c("age: 60", "age: 45", "age: 63", "age: 56", "age: 65"))
For columns 2-5 I'd like to remove "gender: ", "Thing One: ", and "age: " making them the name of their respective columns.
The resulting data frame would be:
Result = cbind.data.frame(
id = c(1, 2 ,3, 4, 5),
gender = c("Female", "Male", "Female", "Male", "Female"),
`Thing One` = c("a", "a", "a", "b", "b"),
age = c("60", "45", "63", "56", "65")
)
To do this I'm running the following function:
re_col = function(i){
new_name = str_split_fixed(i, ": ", 2)[1]
return(assign(new_name, str_split_fixed(i, ": ", 2)[,2]))
}
Through the following applying functions:
plyr::colwise(re_col)(df)
#and
purrr::map(df, re_col)
Without success.
There could also be a much better approach. I initially tried to write a function that could be used with dplyr in data cleaning as a %>% step but was unsuccessful.