1
df_a <- data.frame(matrix(nrow = 4,ncol = 4,1))
df_b <- data.frame(matrix(nrow = 4,ncol = 4,2))
df_c <- data.frame(matrix(nrow = 4,ncol = 4,3))
colnames(df_a) <- colnames(df_b) <- colnames(df_c) <- c("q_1","q_2","q_3")
avg_a <- data.frame(matrix(nrow = 1,ncol = 4))
colnames(avg_a) <- c("q_1","q_2","q_3","q_4")
alph <- c("a","b","c")

I have made 3 datasets and I want to collect averages of all columns of each table in another table. For example average of all columns of df_a will be stored in avg_a. How can I write a loop using alph vector and select datasets in the loop and create dataframes in the loop? I know that using

assign(paste("avg_",alph[i],sep = ""),data.frame(matrix(nrow = 1,ncol = 4)))

allows me to generate average table specific to dataset. but then how I refer to this table such that, for example, each column of avg_a table will have averages from df_a table?

  • 2
    Put your [data frames in a list](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207). – Gregor Thomas May 22 '20 at 12:12
  • 3
    Best option is to put them in a list and loop over that list. Something like `setNames(lapply(list(df_a, df_b, df_c), colMeans), paste0('avg_', c('a', 'b', 'c')))` – Sotos May 22 '20 at 12:14
  • 2
    A general note: Try to avoid foor loops. Use one function from the `apply` family. – MacOS May 22 '20 at 12:53

1 Answers1

0

As @gregor-thomas and @sotos have noted switching from a loop to a list (especially a named list) is clutch. Because the question is still open let me give you a full answer with your example data and using purrr instead of the lapply solution.

library(purrr)
# your example data
df_a <- data.frame(matrix(nrow = 4,ncol = 4,1))
df_b <- data.frame(matrix(nrow = 4,ncol = 4,2))
df_c <- data.frame(matrix(nrow = 4,ncol = 4,3))
colnames(df_a) <- colnames(df_b) <- colnames(df_c) <- c("q_1","q_2","q_3")

# list of your dfs
df_list <- list(df_a, df_b, df_c)
# vector of new names
new_df_names <- c("avg_a","avg_b","avg_c")

# name the list items
df_list <- setNames(df_list, new_df_names)

# this returns vectors of named numbers
purrr::map(df_list, ~ colMeans(.))
#> $avg_a
#>  q_1  q_2  q_3 <NA> 
#>    1    1    1    1 
#> 
#> $avg_b
#>  q_1  q_2  q_3 <NA> 
#>    2    2    2    2 
#> 
#> $avg_c
#>  q_1  q_2  q_3 <NA> 
#>    3    3    3    3

# this goes the additional step of making them dfs
purrr::map(df_list, ~ colMeans(.)) %>% purrr::map(~ as.data.frame(t(.)))
#> $avg_a
#>   q_1 q_2 q_3 NA
#> 1   1   1   1  1
#> 
#> $avg_b
#>   q_1 q_2 q_3 NA
#> 1   2   2   2  2
#> 
#> $avg_c
#>   q_1 q_2 q_3 NA
#> 1   3   3   3  3
Chuck P
  • 3,541
  • 3
  • 6
  • 18