1

i am new in R and i have the following problem: I am working with a data.frame of morethan 3000 obs an 6 variables.

     [PLOT] [SPP]   [tree] [BA] [...] []
[1]     2     A       1       3
[2]     2     B       2       2
[3]     2     C       3       5
[4]     3     A       1       4
[5]     3     C       2       7
[6]     4     B       1       4
[7]     4     D       2       5

[PLOT]and [SPP] are factor, [BA] is numeric I am using:

data1 <- ddply(data,c('PLOT','SPP'),summarise,BAtotal = sum(BA,na.rm=TRUE))

But i want another col with this: %BA per SP and per plot. How it is possible to do? is possible to do with sapply??

Thanks

Inakermo
  • 25
  • 4

1 Answers1

0

We can use mutate instead of summarise

library(plyr)
ddply(data,c('PLOT','SPP'),mutate,BAtotal = sum(BA,na.rm=TRUE))

NOTE This will work only if the 'data' is data.frame and not a matrix. It seems that the OP's data is matrix. In that case, convert to data.frame

data <- as.data.frame(data)

before running the ddply code.

In addition to the above code, we can also use the dplyr methods (should be faster)

library(dplyr)
data %>%
    group_by(PLOT, SPP) %>%
    dplyr::mutate(BAtotal = sum(BA, na.rm=TRUE))

Or data.table

library(data.table)
setDT(data)[, BAtotal := sum(BA, na.rm=TRUE), by = .(PLOT, SPP)]
akrun
  • 874,273
  • 37
  • 540
  • 662