[R] how to loop thru a matrix or data frame , and append calculations to a new data frame?

David Winsemius dwinsemius at comcast.net
Fri Oct 30 17:14:26 CET 2009

On Oct 30, 2009, at 11:46 AM, Robert Wilkins wrote:

> How do you do a double loop through a matrix or data frame , and
> within each iteration , do a calculation and append it to a new,
> second data frame?
> (So, if your original matrix or data frame is 4 x 5 , then 20
> calculations are done, and the new data frame, which had 0 rows to
> start with, now has 20 rows)
Much vagueness in question, Grasshopper:

mtx <- matrix(1:20, 4)
df1 <- data.frame(x=1:20)
df1$mtxsq <- as.vector(mtx)^2


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

More information about the R-help mailing list