Changes to this code will be announced via email and the course FaceBook page CalU EcoStats.

For more labs and tutorials see my WordPress Site lo.brow.R

Why won’t tapply work?

A frequent problem with the tapply() function is that it does not take the common “data = …” argument that many R functions do. You need to explicitly tell it where the data is located.

Generate some fake data

make.my.day.ta(first.initial = "N",
               second.initial = "B")
## [1] "Data for experiment 1 is in the object named experiment1"
## [1] "The mean of the response variable for experiment 1 is 52.2"
## [1] "Type summary(experiment1) to see it"
## [1] "It should look like this:"
##      plant         treatment    photosynth   
##  Min.   : 1.00   control:20   Min.   :46.67  
##  1st Qu.:10.75   worms  :20   1st Qu.:50.91  
##  Median :20.50                Median :52.25  
##  Mean   :20.50                Mean   :52.22  
##  3rd Qu.:30.25                3rd Qu.:53.80  
##  Max.   :40.00                Max.   :57.55  
## [1] ""
## [1] "Data for experiment 2 is in the object experiment2"
## [1] "Data for experiment 3 is in the object experiment3"




Look at fake data

head(experiment1)
##   plant treatment photosynth
## 1     1   control   50.95283
## 2     2   control   49.74924
## 3     3   control   52.19243
## 4     4   control   47.11154
## 5     5   control   52.29566
## 6     6   control   49.06318




For tapply, the NUMERIC variable must come first.

This works

tapply(experiment1$photosynth, #numeric variable
       experiment1$treatment,  #group/treatment/factor etc
       FUN = mean)
##  control    worms 
## 50.49017 53.95676

tapply for some reason DOES NOT take a data arguement -

This code DOES NOT WORK

# tapply(photosynth,
#       treatment,
#       data = experiment1, # there is NO DATA argument in tapply()
#       FUN = mean)