[R] help with loop

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Mar 12 14:41:03 CET 2009


Wacek Kusnierczyk wrote:
> is your data a data frame or a matrix?  do you want to compute the
> differences columnwise, i.e., for each column independently?  consider
> this example:
>
>     # generate and display dummy data
>     (d = as.data.frame(replicate(3, sample(5))))
>
>     # compute successive differences columnwise
>     as.data.frame(apply(d, 2, diff))
>     apply(as.matrix(d), 2, diff)
>
>   

haven't noticed the sum part;  you can apply colSums to the above
resulting data frame or matrix, e.g.:

    colSums(apply(d, 2, diff))

vQ




More information about the R-help mailing list