Doing operation on multiple numbered tables in R

I'm new to programming in R and I'm working with a huge dataset containing hundreds of variables and thousands of observations. Among these variables there is Age, which is my main concern. I want to get means for each other variables in function of Age. I can get smaller tables with this:

for(i in 18:84) 
{
  n<- sprintf("SortAgeM%d",i)
  assign(x=n,subset(SortAgeM,subset=(SortAgeM$AGE>=i & SortAgeM$AGE<i+1)))
}
"SortAgeM85plus"<-subset(SortAgeM,subset=(SortAgeM$AGE>=85 & SortAgeM$AGE<100))

This gives me subdatasets for each age I'm concern with. I would then want to get the mean for each column. Each column is an observation of the volume of a specific brain region. I'm interested in knowing how is the volume decreasing with time and I would like to be able to know if individuals of a given age are close to the mean of their age or not.

Now, I would like to get one more row with the mean for each column. So I tried this:

for(i in 18:85) {
  addmargins((SortAgeM%d,i), margin=1, FUN= "mean")
}  

But it didn't work... I'm stuck and I'm not familiar enough with R function to find a solution on the net... Thank you for your help.

Victor

Post answer edit: This is what I finally did:

for(i in 18:84) 
    {
      n<- sprintf("SortAgeM%d",i)
      assign(x=n,subset(SortAgeM,subset=(SortAgeM$AGE>=i & SortAgeM$AGE<i+1)))
      Ajustment<-c(NA,NA,NA,NA,NA,NA,NA) #first variables aren't numeric
      Line1<- colMeans(item[,8:217],na.rm=TRUE)
      Line<-c(Ajustment,Ligne1)
      assign(x=n, rbind(item,Ligne))
    }

Upvotes: 0

Views: 51

Answers (1)

SmitM
SmitM

Reputation: 1376

If you simply want an additional row with the means of each column, you can rbind the colMeans of your df like this

df_new <- rbind(df, colMeans(df))

Upvotes: 1

Related Questions