0

May I ask for help with R coding? I would like to calculate the simple mean and standard deviation as shown below. This is an example of calculating it for species richness. However, I would like to calculate it for several species, each one is in a separate column (species1, species2, species3, species4 etc).

How can I do it automatically (I guess using some loop or funcion) in R and get a nice overview table where calculations for each species come one by one in below?

mean1=tapply(edge2$species_richness, list (Management =edge2$Management), mean) sd1=tapply(edge2$species_richness, list (Management =edge2$Management), sd)

cbind (mean1, sd1)

Result for species richness:

                  mean1      sd1
AES               15.6250 5.875089
AES2              29.5000 9.570789
Control           6.9375 8.590450
Centre            16.3125 5.437141

I ask help with R coding

user2974951
  • 9,535
  • 1
  • 17
  • 24

1 Answers1

0

Using aggregate on mtcars (the aggregation variable is in the rows, as is yours, the aggregated variables are in the columns)

 > aggregate(.~am,data=mtcars,function(x){c("mean"=mean(x),"sd"=sd(x))})

  am mpg.mean mpg.sd cyl.mean cyl.sd disp.mean disp.sd hp.mean hp.sd drat.mean drat.sd wt.mean
1  0     17.1    3.8      6.9    1.5     290.4   110.2   160.3  53.9       3.3     0.4     3.8
2  1     24.4    6.2      5.1    1.6     143.5    87.2   126.8  84.1       4.0     0.4     2.4
  wt.sd qsec.mean qsec.sd vs.mean vs.sd gear.mean gear.sd carb.mean carb.sd
1   0.8      18.2     1.8     0.4   0.5       3.2     0.4       2.7     1.1
2   0.6      17.4     1.8     0.5   0.5       4.4     0.5       2.9     2.2
user2974951
  • 9,535
  • 1
  • 17
  • 24