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:
Dear useRs,
I'm trying to write a loop to sum my data in the following way:
(the second - the first) + (the third - the second) + (the fourth - the third) + ...
for each column.
So, I wrote something like this:
c <- list()
for(i in 1:ncol(mydata)) {
for(j in 2:nrow(mydata)) {
c[[i]] <- sum(yc[j,i] - yc[(j-1),i])
}}}
As for the columns it works pretty fine, but it only returns the last subtraction, however, I need the sum of all subtractions.
Any ideas?
Regards,
Rafael.
Veja quais s?o os assuntos do momento no Yahoo! +Buscados
[[alternative HTML version deleted]]
------------------------------------------------------------------------
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help