how to pass the name of dataframe list inside a list using a loop in r -


i have list of dataframes try apply function. function should iterate 3 times. after each iteration results should saved on results list.

my data frames have numeric content , different names of columns except last 6 columns (which have same name).

my code follows:

# suposse have 3 df following names myfirstdf  myseconddf mythirddf   mydflist # list containing 3 data frames (i in 1:3){ results[[i]] <- lapply(mydflist, function(x) {          longdata <- ncol(x)-i sum ( x[,1:longdata]) } )  names(results[[i]]) <- sprintf("results[[i]]", 1:length(results)) } 

what want access results of each dataframe adding ith number of iteration, like: results$mydflist$myfirstdfiwhere iwill number of iteration results$mydflist$myfirstdf1. code i've got results$results1$results1

your code produces list of length 3, number of iterations, , each of 3 list items again list of length 3, number of dataframes in mydflist. formulation

what want access results of each dataframe adding ith number of iteration, like: results$mydflist$myfirstdfi number of iteration results$mydflist$myfirstdf1.

in question guess want flat list of length 9, containing 1 item each each iteration step , each dataframe in mydflist, named

"myfirstdf1" "myseconddf1" "mythirddf1"

"myfirstdf2" "myseconddf2" "mythirddf2"

"myfirstdf3" "myseconddf3" "mythirddf3".

the following function can handle both cases:

iteration <- function( dflist, fnct, numberofiterations, flat=true ) {   l <- list()    (i in 1:numberofiterations){     l[[i]] <- lapply( dflist, fnct, )     names(l[[i]]) <- paste0( names(dflist), )   }   return( if (flat) unlist(l,recursive=false) else l ) } 

example:

mydflist <- list(   myfirstdf  = data.frame(matrix(1:20,4,5)),   myseconddf = data.frame(matrix(1:12,2,6)),   mythirddf  = data.frame(matrix(1:15,3,5)) )  f <- function(df,i) {   longdata <- ncol(df)-i   sum(df[,1:longdata]) }  results      <- iteration(mydflist,f,4,false) results_flat <- iteration(mydflist,f,4) 

(i've changed number of iterations 3 4, avoid confusion number of dataframes.) here resulting list results, not flat:

> results [[1]] [[1]]$myfirstdf1 [1] 136  [[1]]$myseconddf1 [1] 55  [[1]]$mythirddf1 [1] 78   [[2]] [[2]]$myfirstdf2 [1] 78  [[2]]$myseconddf2 [1] 36  [[2]]$mythirddf2 [1] 45   [[3]] [[3]]$myfirstdf3 [1] 36  [[3]]$myseconddf3 [1] 21  [[3]]$mythirddf3 [1] 21  [[4]] [[4]]$myfirstdf4 [1] 10  [[4]]$myseconddf4 [1] 10  [[4]]$mythirddf4 [1] 6 

notice number of iteration step appears twice. example result third dataframe in first iteration step is

> results[[1]]$mythirddf1 [1] 78 

in names of flat list results_flat number of iteration step appears once:

> results_flat $myfirstdf1 [1] 136  $myseconddf1 [1] 55  $mythirddf1 [1] 78  $myfirstdf2 [1] 78  $myseconddf2 [1] 36  $mythirddf2 [1] 45  $myfirstdf3 [1] 36  $myseconddf3 [1] 21  $mythirddf3 [1] 21  $myfirstdf4 [1] 10  $myseconddf4 [1] 10  $mythirddf4 [1] 6 

e.g. result third dataframe in first iteration step is

> results_flat$mythirddf1 [1] 78 

if want access result via results$mydflist$mythirddf1, build 1 component list results follows:

> results <- list(mydflist=iteration(mydflist,f,4)) 

the 1 , component of list results list results_flat above, , name mydflist:

> results $mydflist $mydflist$myfirstdf1 [1] 136  $mydflist$myseconddf1 [1] 55  $mydflist$mythirddf1 [1] 78  $mydflist$myfirstdf2 [1] 78  $mydflist$myseconddf2 [1] 36  $mydflist$mythirddf2 [1] 45  $mydflist$myfirstdf3 [1] 36  $mydflist$myseconddf3 [1] 21  $mydflist$mythirddf3 [1] 21  $mydflist$myfirstdf4 [1] 10  $mydflist$myseconddf4 [1] 10  $mydflist$mythirddf4 [1] 6 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -