Add each element in one vector with each element in a second vector

后端 未结 3 982
攒了一身酷
攒了一身酷 2021-01-20 01:48

I would like to add each element in one vector with each element in another vector as follows, but avoid the for loops. Is there a simple method?



        
3条回答
  •  执笔经年
    2021-01-20 02:04

    You can put b in a matrix and take advantage of R's recycling rules:

    a + matrix(b, nrow=length(a), ncol=2, byrow=TRUE)
    

提交回复
热议问题