How do I use R to find sums for values by species? I have 62 species names and want to add the basal area for each species in a habitat. I tried
aggregate(file name, species, FUN=sum, simplify=TRUE)
and many variations on this but it always says that it cannot find species
(a column header in my data set). The list is too long to add as input; I want the program to use my column information. My data set looks something like this:
Species BA
sp1 0.5
sp1 0.2
sp2 0.1
First read the data into a data frame, then use aggregate.
# You would read from the file instead.
x <- read.table(header=T, file=textConnection("Species BA
sp1 0.5
sp1 0.2
sp2 0.1
"))
> aggregate(.~Species, data=x, FUN=sum)
Species BA
1 sp1 0.7
2 sp2 0.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With