[R] loop in function

From: <sumfleth_at_geographie.uni-kiel.de>
Date: Sat, 05 May 2007 17:31:07 +0200 (CEST)


Dear Mailing-List,
I think this is a newbie question. However, i would like to integrate a loop in the function below. So that the script calculates for each variable within the dataframe df1 the connecting data in df2. Actually it takes only the first row. I hope that's clear. My goal is to apply the function for each data in df1. Many thanks in advance. An example is as follows:

df1 <-data.frame(b=c(1,2,3,4,5,5,6,7,8,9,10)) df2 <-data.frame(x=c(1,2,3,4,5), y=c(2,5,4,6,5), z=c(10, 8, 7, 9, 3)) attach(df2)
myfun = function(yxz) (x + y)/(z * df1$b) df1$goal <- apply(df2, 1, myfun)
df1$goal

regards,

kay



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 05 May 2007 - 15:35:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 May 2007 - 06:31:51 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.