[R] (no subject)

jim holtman jholtman at gmail.com
Sat May 5 17:38:20 CEST 2007


>From you function, it looks like you want to return 5 values for each
element in the vector 'df1'.  Is this what you want?  I am not sure
what you expect the output to look like.

> 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))
> goal <- list()
> for (i in df1$b){
+     goal[[i]] <- (df2$x + df2$y)/(df2$z * i)
+ }
> goal
[[1]]
[1] 0.300000 0.875000 1.000000 1.111111 3.333333

[[2]]
[1] 0.1500000 0.4375000 0.5000000 0.5555556 1.6666667

[[3]]
[1] 0.1000000 0.2916667 0.3333333 0.3703704 1.1111111

[[4]]
[1] 0.0750000 0.2187500 0.2500000 0.2777778 0.8333333

[[5]]
[1] 0.0600000 0.1750000 0.2000000 0.2222222 0.6666667

[[6]]
[1] 0.0500000 0.1458333 0.1666667 0.1851852 0.5555556

[[7]]
[1] 0.04285714 0.12500000 0.14285714 0.15873016 0.47619048

[[8]]
[1] 0.0375000 0.1093750 0.1250000 0.1388889 0.4166667

[[9]]
[1] 0.03333333 0.09722222 0.11111111 0.12345679 0.37037037

[[10]]
[1] 0.0300000 0.0875000 0.1000000 0.1111111 0.3333333



On 5/5/07, sumfleth at geographie.uni-kiel.de
<sumfleth at geographie.uni-kiel.de> wrote:
> 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list