[R] Element-by-element operation (adding)

peter dalgaard pdalgd at gmail.com
Mon May 23 09:20:06 CEST 2016


> On 23 May 2016, at 07:44 , Peter Langfelder <peter.langfelder at gmail.com> wrote:
> 
> or
> 
> t(apply(v, 1, `+`, b))

Or, as you're messing with transposes anyways, use the fact that the column-wise counterpart is automagically handled by recycling:

t(t(v)+b)

Or, look Ma, no transposes
 
v + rep(b, each=nrow(v))

(_always_ doublecheck the logic when you apply these and similar techniques! I have seen my share of student code where recycling had been applied along the wrong dimension of a matrix...)

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list