I will try to put it in this way:
I’m making a sum :
tot <- fac$a[,1]+fac$b[,1]
Where a and b are factors and 1 the first column. And until here it’s fine.
Now I would like to use a for loop in order to do the sum for the all columns:
tot <- 0
for (i in 5:13) {
tot[,i] <- fac$a[,i]+fac$b[,i]
}
Which is not working.
Any suggestion?
Thanks.
I only can guess your problem. (You forgot the error message.)
IMHO it isn’t working because
totis of class numeric and no matrix (and has no columns neither any rows which could access by indexi).To fix your code:
tot <- matrix(ncol=ncol(fac$a), nrow=nrow(fac$b))BTW you should not use a for loop for this kind of calculation. Use vectorization instead: