-2

I'm in a class learning R right now, and I'm stuck on a problem.

We set up the following data frame:

Column1=c(rep("control",300),rep("ConditionB",300),rep("ConditionC",300))
Column2=(900,mean=100,sd=10)
data=data.frame(Column1,Column2)

It runs fine, but then he asked us the following request:

"Use tapply to find the means of "control", "ConditionB", and "ConditionC"".

I have tried ten different ways of putting it, and I just come out with errors. Can anyone help me with this?

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    I guess you missed a `rnorm` in your second line. Just try `tapply(data$Column2,data$Column1,mean)` – nicola Dec 20 '14 at 22:11
  • possible duplicate of [Getting the mean value for every Id in a data frame](http://stackoverflow.com/questions/3797083/getting-the-mean-value-for-every-id-in-a-data-frame) – Henrik Dec 20 '14 at 22:18

2 Answers2

1

As mentioned in the comments, you missed an rnorm(). You can also use the vector c("control","ConditionB","ConditionC") and times = 300 instead of repeating rep() 3 times.

Column1=rep(c("control","ConditionB","ConditionC"), times = 300)
Column2=rnorm(900,mean=100,sd=10)
data=data.frame(Column1,Column2)
tapply(data$Column2,data$Column1,mean)
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
1

In addition to use tapply, try hadley's package dplyr:

data %>% group_by(Column1) %>% summarise(mean(Column2))

yang
  • 719
  • 3
  • 11