0

Suppose I have a dataframe with 16 variables named : Amount_day_1, Amount_day_2 .. I would like to take the mean of each

What I thought of doing is :

for (i in 1:16){
  !!as.name(paste0("Mean_Amount_Day_", i) <- 
         mean(df$!!as.name(paste0("Amount_Day_", i)))
}

But it really does not work. In fact I really don't know how to select a variable with the dollar sign if it is within a for loop?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Lola1993
  • 151
  • 6
  • https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value and https://stackoverflow.com/questions/12389318/dollar-sign-before-a-variable seem relevant – user20650 May 24 '21 at 23:22

1 Answers1

3

We could do this with colMeans to create a single named vector of means

colMeans(df, na.rm = TRUE)

NOTE: It is not recommended to create multiple objects in the global env


In a for loop, if we are creating objects use assign

for(i in seq_along(df)) {
     assign(paste0("Mean_Amount_Day_", i), mean(df[[i]], na.rm = TRUE))
 }

columns can be subset by either column name or by index. As the OP mentioned that they need the mean of all the columns and is looping by index, use the index to extract the column. Also, there is no need to convert to symbol with as.name nor use any !!. Instead, use [[ for subsetting

akrun
  • 874,273
  • 37
  • 540
  • 662