0

I am using tapply to compress a large dataset on multiple species of plants grown under multiple conditions, in four separate experiments. tapply then spits an output summarized by species. I am trying to then pull these separate species out of the output for further analyses. So, tapply gives me this (from some dummy data) :

> tapply(results, list(trial,condition,species),mean)
> 
, , species1

       A          B           C          D
1 -0.6357911  0.6127278 -0.23812194 -0.2769131
2 -0.3851283 -0.5955274 -0.08072294 -0.7298832
3  0.2029780 -1.0282842  0.11518872 -0.6522809
4 -0.2254586  0.4215911 -0.84305584 -0.1108188

, , species2

       A           B           C           D
1 -0.4762501  0.35766102 -0.53821633 -0.64798979
2  0.0558234  0.18602479 -0.48208241  1.09532972
3 -1.0695515  0.84401536 -0.02232301 -0.02064807
4 -0.2423312 -0.02145042 -0.18834442 -0.08221573

Where I get the mean of each species, under each condition (A-D), run in 4 separate experiments (1-4). Is it then possible, to, say, isolate species A, and then average 1-4 for each column?

  • It would be easier to help you if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output for that input so we can test possible solutions. – MrFlick Mar 24 '17 at 21:04

1 Answers1

0

Not fully sure how your data is structured. but here's an attempt.

 #Data generation function runif
 data_gen <- function() {
   x <- 
    data.frame(A=runif(4, -1.005, 1.0049),
       B=runif(4, -1.005, 1.0049),
       C=runif(4, -1.005, 1.0049),
       D=runif(4, -1.005, 1.0049))
   return(x)
  }

#Shape to similar structure
data <- list(species1=data_gen(),
         species2=data_gen(),
         species3=data_gen(),
         species4=data_gen())

#1.lapply break it up to column 1 == A 
#2.then sapply with a mean then transpose to 1 row
#3.and finally convert to data.frame
data_transf <- data.frame(t(sapply(lapply(data, '[[', 1),mean)))

#Rename columns if necessary
colnames(data_transf) <- paste0(colnames(data_transf),'_A_isolated_avg')

data_transf
jg_r
  • 81
  • 5