I'm new to R. I need to calculate means of variable for a regular interval for each individual. I have this simple data frame.
df = data.frame(id=c("A","B","C","D"),
x1=c(3,5,7,2), x2= c(5,3,7,3), x3=c(5,6,4,4), x4=c(5,3,7,3),
x5=c(5,3,7,3), x6=c(5,4,7,1), x7= c(5,7,7,3), x8= c(5,3,8,3),
x9= c(4,3,2,3))
What I want to calculate is means of every 4th column (eg: mean for x1, x4, x7, variables for each individual and x2, x5, x8 for each individual etc.)as a new data frame. My output should be as the following.
y1 y2 y3
A 4.333333 5.000000 4.666667
B 5.000000 3.000000 4.333333
C 7.000000 7.333333 4.333333
D 2.666667 3.000000 2.666667
In the actual data frame I have 120 variables and 40 individuals.
I searched for previous posts and try to do it in the following way. But surely the way I apply is wrong.
df2<-with(df,(seq([,2], [,10], by=3)),FUN= function(x) mean(x, na.rm=TRUE))
Thank you for any advice.