Skip to content
Prev 173474 / 398506 Next

help with loop

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)

see ?apply and ?diff for details.  note that apply will return a matrix
both when given a data frame and when given a matrix.

vQ
Rafael Moral wrote: