Given a (simplified) dataframe with format
df <- data.frame(a = c(1,2,3,4), b = c(4,3,2,1), temp1 = c("-","-","-","foo: 3"), temp2 = c("-","bar: 10","-","bar: 4") ) a b temp1 temp2 1 4 - - 2 3 - bar: 10 3 2 - - 4 1 foo: 3 bar: 4 I need to rename all temp columns with the names contained within the column, My end goal is to end up with this:
a b foo bar 1 4 - - 2 3 - 10 3 2 - - 4 1 3 4 the df column names and the data contained within them will be unknown, however the columns that need changing will contain temp and the delimiter will always be a ":"
As such I can easily remove the name from within the columns using dplyr like this:
df <- df %>% mutate_at(vars(contains("temp")), ~(substr(., str_locate(., ":")+1,str_length(.)))) but first I need to rename the columns based on some function method, that scans the column and returns the value(s) within it, ie.
rename_at(vars(contains("temp")), ~(...some function.....)) As per the example given there's no guarantee that specific rows will have data so I can't simply grab value from row 1
Any ideas welcome. Thanks in advance
foo/barcolumns to be numeric, or stay as character columns?