I am writing a function that uses a list argument. As part of this function, I will convert the list to a data frame with stack()
. Now, to my question, how can I rename the column of the resulting data frame (or any other, as necessary for the real function) with the name of the list that I use in the function argument?
My function:
stack_and_rename <- function(list) {
stacked_list <- stack(list)
names(stacked_list)[names(stacked_list)=='ind'] <- 'list'
stacked_list
}
Dummy list:
fields <- list(A = c(2:4, 12:16, 24:28, 36:40, 48:49),
B = c(6:10, 18:22, 30:34, 42:46))
But the call stack_and_rename(fields)
obviously returns a data frame with column values and a “list”, although I would like to get “values” and “fields”. I have to make some link errors, but I just can't get a solution. Thank you
Tjebo source
share