Search code examples
rlistplyrcombn

Assign names to dataframes within a list based on combn


I have created a list of dataframes using alply (~500), in the manner;

prevalence <- alply(combos, 2, prevalence.func)

I'd like to assign new names to each data.frame based on the same combos, replacing the default numeric names 1,2,3...500.

Reproducible example created using:

simple_list = replicate(n = 10,
                 expr = {data.frame(x = rnorm(20), y = rnorm(20))},
                 simplify = F)

combos <-combn(1:5, 2)

So that the names of each dataframe...

simple_list$1 becomes simple_list$1-2
simple_list$10 becomes simple_list$4-5

...which tells me over which interval each data.frame was computed across in the original function.

I tried using lapply with;

someFunction <-function (x){

  names(simple_list)<-paste(combos[x,x[1]],combos[x,x[2]],sep="-")

 }

lapply(simple_list,someFunction)

But I recieve there error:

Error in combos[x, x[1]] : invalid subscript type 'list'

Something is obviously not right with my assignment function. Any tips?


Solution

  • The combn has a FUN argument, so it can be

     names(simple_list) <- combn(5,2, FUN= paste, collapse='-')
     names(simple_list)
     #[1] "1-2" "1-3" "1-4" "1-5" "2-3" "2-4" "2-5" "3-4" "3-5" "4-5"