How can I sum the values โ€‹โ€‹in a column, grouped by names in rows in R, without specifying each name?

How to use R to find sums for values โ€‹โ€‹by species? I have 62 species names and you want to add a basal area for each species in the habitat. I tried

aggregate(file name, species, FUN=sum, simplify=TRUE) 

and many variations, but he always says that he cannot find the species (column heading in my dataset). The list is too long to add as input; I want the program to use my column information. My dataset looks something like this:

 Species BA sp1 0.5 sp1 0.2 sp2 0.1 
+4
source share
2 answers

Read the data in the data frame first, then use the 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 
+5
source

The data.table package data.table great for this kind of material.

First download the package and create some data.

 library(data.table) set.seed(1) dat <- data.table(Species = paste("s", sample(1:3, 15, replace = TRUE), sep = ""), BA = rnorm(15), CA = rnorm(15), DA = rnorm(15), EA = rnorm(15), key="Species") dat # Species BA CA DA EA # 1: s1 -0.005767173 0.80418951 1.2383041 -0.79533912 # 2: s1 -1.147657009 -0.69095384 -0.4527840 -0.17262350 # 3: s1 -0.891921127 -0.43331032 1.1565370 -0.94064916 # 4: s1 0.435683299 -0.64947165 0.8320471 -0.11582532 # 5: s1 -1.237538422 0.72675075 -0.2273287 -0.81496871 # 6: s2 2.404653389 -0.05710677 -0.2793463 -0.05487747 # 7: s2 0.763593461 0.50360797 1.7579031 0.25014132 # 8: s2 -0.411510833 -0.23570656 -1.0655906 0.35872890 # 9: s2 0.252223448 -0.54288826 -1.5637821 -0.01104548 # 10: s2 0.377395646 0.99216037 -0.3767027 -1.42509839 # 11: s3 -0.799009249 1.08576936 0.5607461 0.61824329 # 12: s3 -0.289461574 -1.28459935 -0.8320433 -2.22390027 # 13: s3 -0.299215118 0.04672617 -1.1665705 -1.26361438 # 14: s3 -0.224267885 1.15191175 0.2661374 0.24226348 # 15: s3 0.133336361 -0.42951311 2.4413646 0.36594112 

Note. If you already have data.frame (which I assume you are doing), you can simply use data.table(YourDataFrame, key=YourGroupingColumns)

Here is the actual aggregation. .SD is a subset of the columns of your data. By default, all columns in your data exclude key (your grouping column, in this case we specified โ€œViewsโ€ as our key).

 dat[, lapply(.SD, sum), by=key(dat)] # Species BA CA DA EA # 1: s1 -2.847200 -0.2427955 2.546776 -2.8394058 # 2: s2 3.386355 0.6600668 -1.527519 -0.8821511 # 3: s3 -1.478617 0.5702948 1.269634 -2.2610668 

However, there is also the .SDcols argument, which allows you to specify which columns you are interested in either by name or by numerical index.

 dat[, lapply(.SD, sum), by=key(dat), .SDcols = "DA"] # Species DA # 1: s1 2.546776 # 2: s2 -1.527519 # 3: s3 1.269634 dat[, lapply(.SD, sum), by=key(dat), .SDcols = 2:3] # Species BA CA # 1: s1 -2.847200 -0.2427955 # 2: s2 3.386355 0.6600668 # 3: s3 -1.478617 0.5702948 # or perhaps, more easily understood. dat[, lapply(.SD, sum), by=key(dat), .SDcols = c('BA','CA')] # Species BA CA # 1: s1 -2.847200 -0.2427955 # 2: s2 3.386355 0.6600668 # 3: s3 -1.478617 0.5702948 
+4
source

All Articles