Hi, I'm new to this forum and would be so very grateful if you could help me.
I am trying to create means for a few variables in a loop function (data sample is below).
(i.e mean of ff_l4l5 and ff_l5s1, mean of gc_l4l5 and gc_5s1)
I am able to do this one variable a time by searching for a stem (i.e. either ff_ or gc_) but not sure how to do this in a loop.
Could you help me create a function that loops though the stems? (similar to foreach function in STATA)
Here is the code that works for one stem:
library(dplyr)
df <- df %>%
mutate(ff_ave = rowMeans(x = select(.data = .,
starts_with(match = "ff_"))))
Here is the data sample:
id ff_l4l5 ff_l5s1 gc_l4l5 gc_l5s1 volume_l4l5 volume_l5s1
1 24.16 30.32 2 2 28203.11 12173.37
2 7.61 12.27 0 0 29032.61 18699.53
3 3.77 7.87 1 1 16189.05 7394.11
4 18.67 23.86 2 2 37662.74 16002.27
5 16.59 31.86 2 2 26130.15 13880.79
Thank you so much! Appreciate your help.
...