-2

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.

sriya
  • 179
  • 1
  • 2
  • 7

2 Answers2

1

For the data frame shown in the example you can use the following code

new.df <- data.frame(id = c("A", "B", "C", "D"))
for (i in 2:4) {
  id <- seq(i, ncol(df), 3)
  new.df[, i] <- rowMeans(df[,id])
}

Just extend it to your actual data frame. Maybe a for loop is not the best solution but is the first that came in my mind.

1

We could also use lapply with Reduce

n <- 3
Reduce(`+`,lapply(seq(2, ncol(df), by =n), 
          function(i) df[i:( min(c(ncol(df), i+n-1)))]))/n
#       x1       x2       x3
#1 4.333333 5.000000 4.666667
#2 5.000000 3.000000 4.333333
#3 7.000000 7.333333 4.333333
#4 2.666667 3.000000 2.666667
akrun
  • 874,273
  • 37
  • 540
  • 662