0

I am trying to create a for loop where it calculates the mean of an already existing variable. The data frames are titled "mali2013", "mali2014", "mali2015", "mali2016", and "mali2017" and the variable is prop_AFR. I am trying to calculate the mean of variable per data frame.

I tried

    for (i in 2014:2017) {
      variable = paste0("mali", Year, "$prop_AFR")
      M_mean_AFR_data <- mean(as.numeric(variable), na.rm = TRUE)
      assign(paste0("Mali_prop_AFR_", i), M_mean_AFR_data)
    }

but it kept yielding NaN. Is there any way to put this in a loop, or should I just do it manually?

  • 2
    Look into [list of frames](https://stackoverflow.com/a/24376207/3358227), I suggest you move your `mali....` frames into a single named-list, perhaps with `mali <- mget(ls(pattern="^mali"))`, then use `mean(as.numeric(mali[[Year]]$prop_AFR), na.rm=TRUE)`. – r2evans Jan 30 '23 at 19:10
  • Or use ```variable <- eval(parse(text = variable))``` after first line for minimal change. – one Jan 30 '23 at 19:12
  • 1
    If you are new to R, you should avoid `get` and `assign`. Those are very much anti-patterns in R. It's not helpful to create a bunch of variables in your global namespace with data in their names. R works much better if you store related data in lists or data.frames and apply transformations to those collections. – MrFlick Jan 30 '23 at 19:25
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 30 '23 at 21:42

1 Answers1

0

It looks like Stata style code to me. In R, there might be several simpler ways to do it without looping. I would try this:

library(dplyr)
df <- bind_rows(mali2013, mali2014, mali2015, mali2016, mali2017)
df %>% group_by(Year) %>% 
  summarize(prop_AFR = mean(prop_AFR, na.rm = TRUE) 
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27