0

I would like to calculate the z scores of a summed scale based on two factors: Gender, and Age group (four factors).

How can I do that in R? I'm really new to R, and only started learning, I came across

tapply(AgingData$StandMen, list(AgingData$AgeGroups, AgingData$Gender_2),
      FUN= "scale")

But the result is not a data frame. How can I turn it into a data frame? Or is there any other way?

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Welcome to SO. Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – agstudy Dec 30 '13 at 06:02

1 Answers1

0

The answer can be better if you provide a reproducible example and you show the expected output. That's said, the result of tapply

res <- tapply(AgingData$StandMen, list(AgingData$AgeGroups, AgingData$Gender_2),
      FUN= "scale")

is an array with dimensions:

nelevls(AgingData$AgeGroups) x nlevles(AgingData$Gender_2)

Here you can convert it to a data.frame using

as.data.frame(res)

But I doubt is that what you are looking for, I guess you want to get the result in the long format either:

You use reshape2 package to transform the result :

library(reshape2)
melt(res)

Or you use , you don't use tapply, and you try ddply from plyr:

library(plyr)
ddply(AgingData,.(AgeGroups,Gender_2),transform,scale)
agstudy
  • 119,832
  • 17
  • 199
  • 261