vectorise rows of a dataframe, apply vector function, return to original dataframe r -
given following df:
a=c('a','b','c') b=c(1,2,5) c=c(2,3,4) d=c(2,1,6) df=data.frame(a,b,c,d) b c d 1 1 2 2 2 b 2 3 1 3 c 5 4 6
i'd apply function takes vector (and returns vector) cummax
row row columns in position b
d
.
then, i'd have output in df, either vector in new column of df, or replacing original data.
i'd avoid writing for
loop iterate every row, pull out content of cells vector, thing , put back.
is there more efficient way? i've given apply family functions go, i'm struggling first way vectorise content of columns row , right output.
the final output (imagining i've applied cummax() function).
b c d 1 1 2 2 2 b 2 3 3 3 c 5 5 6
or
b c d output 1 1 2 2 (1,2,2) 2 b 2 3 1 (2,3,3) 3 c 5 4 6 (5,5,6)
where output vector.
seems simple apply
problem want cbind df:
> cbind(df, apply(df[ , 4:2] # work columns in reverse order , 1, # row-by-row cummax) ) b c d 1 2 3 d 1 2 2 2 1 6 c b 2 3 1 2 3 6 b c 5 4 6 2 3 6
ouch. bitten failing notice returned in column oriented matrix , need transpose result; such newbie mistake. show value of having question reproducible dataset suppose.
> cbind(df, t(apply(df[ , 4:2] , 1, cummax) ) ) b c d d c b 1 1 2 2 2 2 2 2 b 2 3 1 1 3 3 3 c 5 4 6 6 6 6
to destructively assign result df use:
df <- # .... code.
this concatenation commas (and result no longer needs transposed:
> cbind(df, output=apply(df[ , 4:2] , 1, function(x) paste( cummax(x), collapse=",") ) ) b c d output 1 1 2 2 2,2,2 2 b 2 3 1 1,3,3 3 c 5 4 6 6,6,6
Comments
Post a Comment