0

I have seen this reshape problem several times on SO but cannot find a solution to my particular problem;

I have a data frame called DF (below) and I calculated the mean and standard deviation using cast () for all rows and columns of interest.

       Bulb.means.sd<-cast(DF, Bat.Species~Bulb.Type, c(mean,sd), value="Bat.Passes")

enter image description here

I am attempting to cast() the raw values from DF, which I would like to incorporate as a single column into the data frame (above) with the same wide format. However, cast() is producing the error message below. Once the raw values have been assimilated, I was thinking of using of using ddply to calculate the standard error (calculation below) for each rows from the raw values.

 Bulb.raw<-cast(DF, Bat.Species~Bulb.Type, value='Bat.Passes')

This error message:

   Aggregation requires fun.aggregate: length used as default

My attempt with ddply literally prints out the same values contained in column Control_sd found in the data frame Bulb.means.sd

 se <- ddply(Bulb.means.sd, c("Bat.Species"), summarise,
                                     N  = length(Control_sd),
                                    se  = Control_sd / sqrt(N))

Does anyone have any suggestions with how to produce a data frame like Bulb.means.sd containing the mean, standard, deviation and standard error for each each row using one line of code, or does anyone know what is wrong with code (above) using the function ddply()?

Many thanks in advance if anyone can help, it is deeply appreciated.

DF<-structure(list(Bat.Species = structure(c(2L, 8L, 9L, 7L, 6L, 
5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 
2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 
5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 
2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 
5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 
2L, 8L, 9L, 7L, 6L, 5L, 3L, 1L, 4L, 10L, 2L, 8L, 9L, 7L, 6L, 
5L, 3L, 1L, 4L, 10L), .Label = c("Barbestrellus barbestrellus", 
"Eptesicus.serotinus ", "Myotic brandii", "Myotis daubentonii", 
"Nyctalus leileri", "Nyctalus noctule", "Pipestrellus Nasthusii", 
"Pipestrellus pipestrelle", "Pipestrellus pygmaeus", "Pleocutus auritus "
), class = "factor"), Bat.Passes = c(0L, 0L, 0L, 0L, 0L, 0L, 
 3L, 0L, 9L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 0L, 0L, 0L, 0L, 0L, 
 0L, 4L, 0L, 0L, 3L, 0L, 9L, 0L, 0L, 0L, 7L, 0L, 0L, 0L, 6L, 0L, 
 0L, 0L, 0L, 86L, 7L, 17L, 0L, 48L, 6L, 2L, 0L, 0L, 0L, 86L, 28L, 
 8L, 50L, 48L, 1L, 2L, 0L, 0L, 0L, 20L, 0L, 0L, 0L, 0L, 0L, 2L, 
 1L, 0L, 0L, 8L, 0L, 0L, 3L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 
 2L, 0L, 0L, 0L, 1L, 5L, 0L, 10L, 1L, 0L, 2L, 0L, 0L, 0L, 1L, 
 5L), Bulb.Type = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =    c("Control", 
 "LED", "Metal.Halide", "SOX"), class = "factor")), .Names =     c("Bat.Species", 
"Bat.Passes", "Bulb.Type"), class = "data.frame", row.names = c(NA, 
-100L))
aosmith
  • 34,856
  • 9
  • 84
  • 118
Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31
  • You have multiple observations for some species and bulb.type combinations. If your `cast` formula doesn't lead to unique identification of values, `cast` defaults to aggregation (hence the warning message). If you want to cast wide without aggregation, you'll need some additional variable so the rows of the wide dataset are uniquely identified. – aosmith Sep 27 '16 at 18:15
  • Hi Aosmith, Thank you for your advice. So, I have solved the cast() problem by manipulating my data frame. Would you happen to know how to calculate the standard error per row from the s.d using the R code above (object: 'Bulb.means.sd'). This information would be greatly appreciated. – Alice Hobbs Sep 29 '16 at 18:55

0 Answers0