0
for(i in unique(IsraelData_QTR1$year)) {
  nam <- paste("IsraelData_QTR1", i, sep = ".")
  assign(nam, IsraelData_QTR1[IsraelData_QTR1$year==i,])
}

I would like to use the mean function to calculate the mean of a column something like this, using a loop to create several means with changing variable name:

Mean_QTR1.81 <- mean(IsraelData_QTR1.81$DailyHoursWorked)
Mean_QTR1.82 <- mean(IsraelData_QTR1.82$DailyHoursWorked)

The 81 is because the loop goes between 81 and 91 for the names of each new dataframe.

Another problem I thought about is that I would need to do something to change the name of the variable each time depending on the name of the new dataframe. Is there any way to do this ?

Any help is appreciated

Carlos Menem
  • 49
  • 10
  • Try `aggregate(DailyHoursWorked ~ year, data = IsraelData_QTR1, FUN = mean)`. It returns a `data.frame` with two columns: `year` and `DailyHoursWorked` (which are the means of each year's data). It's generally better to store these aggregations in a single object (list, frame or vector) instead of individual variables, since what you do to one is likely to be done to most or all of the others. – r2evans Jul 08 '21 at 16:15
  • 1
    Thanks @r2evans ! It works perfectly – Carlos Menem Jul 08 '21 at 19:33

0 Answers0