I am starting to using R and it's really probably that my question is simple one, but nonetheless I have spent a lot of time trying to figure out what I am doing wrong and to no avail.
I have to thank you because I discover this site last week searching through other questions. But now as someone new, it is often difficult to interpret other people's code.
My version of RStudio is: 1.1.442
My question is that I have two data frames one with some years and one with some items that were found in severals trawls , and I need to summarize items and make another variable. Which appear the summarize of items per year and trawl.
So, I made a loop with for to have the same bottom trawls and the same year in order to sum items.
I made a simplification of my data base.
BT<- c(1, 1, 2, 2, 2, 3, 3, 3, 3, 3)
YEAR<- c(2007, 2007, 2008, 2008, 2008, 2009, 2009, 2009, 2009, 2009)
W<- c(95, 6, 60, 50, 4, 21, 56, 44, 23, 4)
Data1= data.frame(BT,YEAR,W)
Trawl<- c(1, 2, 3)
Year<- c(2007, 2008, 2009)
Data2= data.frame(Trawl,Year)
peso=cbind()
for(i in 1:length(Data1$BT)) {
ind=which(Data2$Trawl == Data1$BT[i] & Data2$Year == Data1$YEAR[i])
print(i)
print(ind)
print(Data1$W[ind])
peso[i]=Data1$W[ind]
sumaGr[i]=colSums(peso[i])
}
And I get this:
Error in colSums(peso[i]) : 'x' must be an array of at least two dimensions
But I don't know how to fix it. I would appreciate all your help and advices. Thank you in advance.