0

I am new to R so my title might not even make sense. Basically, I have this data set, which is the length of insects collected at 2 different sites. When I used the 'summary' function, I got values for both sites, and I'd like to do calculate the mean of each site:

> ls()
[1] "simulies"
> ls.str()
simulies : 'data.frame':        615 obs. of  2 variables:
 $ Site  : Factor w/ 2 levels "Lovering","Orford": 2 2 2 2 2 2 2 2 2 2 ...
$ Length: num  4.1 2.8 2.2 3.2 3.2 3.1 3.1 4.1 4.1 3.2 ...
> summary(simulies)
   Site         Length     
Lovering:398   Min.   :0.600  
Orford  :217   1st Qu.:1.300  
               Median :1.600  
               Mean   :2.034  
               3rd Qu.:2.700  
               Max.   :5.000 
Ben
  • 1
  • 1
  • Using just base R one can use the tapply function: `tapply(simulies, simulies$Site, FUN=mean)` – Dave2e Oct 04 '17 at 17:21
  • Well, in base that would be `tapply(simulies$Length, simulies$Site, FUN = mean)`. Ben, I see you edited your question - are you having trouble applying the answers from the dupe to your own case? Or are you good? – Gregor Thomas Oct 04 '17 at 21:18
  • Yes I was trying to refer to the duplicate and it didn't work either. However, I got some help in the comments below, I was missing the capital S in "Site". – Ben Oct 04 '17 at 21:32

1 Answers1

0

There are many different options. Two of them:

To get the mean of one site:

mean(simulies$Length[simulies$Site=="Lovering"])

Or using ddply to get output for both sites:

library(plyr)
ddply(simulies, .(Site), summarize, Mean=mean(Length))
user3640617
  • 1,546
  • 13
  • 21