Skip to content

loop question

1 message · Thomas Friedrichsmeier

#
Dear all,

I am trying to implement the following in a loop:
g <- cbind(c(1, 2, 3), c(1, 2, 3))
h <- cbind(c(1, 2, 3), c(1, 2, 3))
c <- cbind(g[,1]*h[,1], g[,2]*h[,2])
g<-rowSums(c)

My attempt looks like this but does not produce the desired results as 
above.

for (i in 1:2) {g<- rowSums(cbind(g[,i]*h[,i]))}

How do I do this correctly?

Many thanks,

Thomas