This is my first time ever using R so please forgive me if this question is not worded properly. I have a .csv file that I imported into R and I am trying to summarize some data. each row of data if for a given year, study site, and area and each column has the number of species present. There are 4 columns for each species as there were 4 surveys where the species could have been seen.
I am trying to get the sum of each species by year and study site. Columns 5:8 are one species, 9:12 another, 13:16 another and so on. Here is the code that I thought would summarize columns 5:8 by year (YYYY) and study area (SAR):
aggregate(test[,5:8],by = list("SAR","YYYY"), FUN = sum, na.rm = TRUE)
This gives me the error message that "argument must have the same length". Can anyone help me through this initial step?
Here is some of the data:
SAR YYYY GRID_ID WID col1 col2 col3 col4
BCPALP 2005 1 1189 NA NA 0 0
BCPALP 2005 1 1190 0 NA 0 0
BCPALP 2005 1 1191 0 0 NA NA
BCPALP 2005 1 1192 0 NA NA NA
BCPALP 2005 1 1194 NA NA 1 NA
BCPALP 2005 1 1195 NA NA 1 NA
BCPALP 2005 1 1196 0 NA 0 NA
BCPALP 2005 1 1198 0 NA 0 NA
BCPALP 2005 1 1199 0 NA 0 0
I'm hoping to get an output that is something like this:
SAR YYYY total of columns 1:4
BCPALP 2005 2
This is the code I just tried.
aggregate(cbind("col1", "col2", "col3", "col4")~SAR+YYYY, test, FUN=sum, na.rm=TRUE, na.action=NULL)
It gives me an error message that states "variable lengths differ (found for 'SAR')".
I went back and checked the data and all the variable lengths are the same.