[R] Loop and function
Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Loop and function
Exactly how are you accessing it and what warnings are you getting. Your loop is just returning a single value; the last time i=12. If you want a vector of values back, the you have to do: returnlow - numeric(12) for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow[i] - gpdlow(var[,i][var[,i](p[,i][[2]]) } On 7/5/07, livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Loop and function
You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Loop and function
Thanks a lot. I have corrected this. But it still does not work. Any thought? Stephen Tucker wrote: You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11445807 Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Loop and function
What does gpdlow(var[,i][var[,i](p[,i][[2]]) return? Is it a vector; if so, how long? Your declaration of returnlow- matrix(,12) str(returnlow) logi [1:12, 1] NA NA NA NA NA NA ... is a matrix of 12 rows and one column. You may be getting the error message is gpdlow is returning a vector longer than one. Do str(gpdlow(var[,i][var[,i](p[,i][[2]])) so that we can see what the data looks like. You still haven't provided a self-contained example, so we can only guess at what is happening. On 7/5/07, livia [EMAIL PROTECTED] wrote: Thanks a lot. I have corrected this. But it still does not work. Any thought? Stephen Tucker wrote: You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11445807 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Loop and function
I tried str(gpdlow(var[,i][var[,i](p[,i][[2]])) and it returns num [1:49] -1.92 -1.69 -2.20 -1.65 -2.13 ... It is the number when i=1, I guess it does not loop. In fact, the number should be different when loop between i. jim holtman wrote: What does gpdlow(var[,i][var[,i](p[,i][[2]]) return? Is it a vector; if so, how long? Your declaration of returnlow- matrix(,12) str(returnlow) logi [1:12, 1] NA NA NA NA NA NA ... is a matrix of 12 rows and one column. You may be getting the error message is gpdlow is returning a vector longer than one. Do str(gpdlow(var[,i][var[,i](p[,i][[2]])) so that we can see what the data looks like. You still haven't provided a self-contained example, so we can only guess at what is happening. On 7/5/07, livia [EMAIL PROTECTED] wrote: Thanks a lot. I have corrected this. But it still does not work. Any thought? Stephen Tucker wrote: You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11445807 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11446873 Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Loop and function
Thanks. Yes, gpdlow is indeed return a vector longer than one. The length of the vector is different for i in 1:12, each equals to length(var[,i][var[,i](p[,i][[2]]). jim holtman wrote: What does gpdlow(var[,i][var[,i](p[,i][[2]]) return? Is it a vector; if so, how long? Your declaration of returnlow- matrix(,12) str(returnlow) logi [1:12, 1] NA NA NA NA NA NA ... is a matrix of 12 rows and one column. You may be getting the error message is gpdlow is returning a vector longer than one. Do str(gpdlow(var[,i][var[,i](p[,i][[2]])) so that we can see what the data looks like. You still haven't provided a self-contained example, so we can only guess at what is happening. On 7/5/07, livia [EMAIL PROTECTED] wrote: Thanks a lot. I have corrected this. But it still does not work. Any thought? Stephen Tucker wrote: You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11445807 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11446408 Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Loop and function
Please show use the statements that you used. Did you put the 'str' inside the loop? It is hard to tell what is happening without reproducible code. On 7/5/07, livia [EMAIL PROTECTED] wrote: I tried str(gpdlow(var[,i][var[,i](p[,i][[2]])) and it returns num [1:49] -1.92 -1.69 -2.20 -1.65 -2.13 ... It is the number when i=1, I guess it does not loop. In fact, the number should be different when loop between i. jim holtman wrote: What does gpdlow(var[,i][var[,i](p[,i][[2]]) return? Is it a vector; if so, how long? Your declaration of returnlow- matrix(,12) str(returnlow) logi [1:12, 1] NA NA NA NA NA NA ... is a matrix of 12 rows and one column. You may be getting the error message is gpdlow is returning a vector longer than one. Do str(gpdlow(var[,i][var[,i](p[,i][[2]])) so that we can see what the data looks like. You still haven't provided a self-contained example, so we can only guess at what is happening. On 7/5/07, livia [EMAIL PROTECTED] wrote: Thanks a lot. I have corrected this. But it still does not work. Any thought? Stephen Tucker wrote: You do not have matching parentheses in this line returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) most likely there is a syntax error that halts the execution of the assignment statement? --- livia [EMAIL PROTECTED] wrote: Hi All, I am trying to make a loop for a function and I am using the following codes. p and var are some matrix obtained before. I would like to apply the function gpdlow for i in 1:12 and get the returnlow for i in 1:12. But when I ask for returnlow there are warnings and it turns out some strange result. for (i in 1:12){ gpdlow - function(u){ p[,i]$beta -u*p[,i][[2]] } returnlow - gpdlow(var[,i][var[,i](p[,i][[2]]) } -- View this message in context: http://www.nabble.com/Loop-and-function-tf4028854.html#a11443955 Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11445807 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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. -- View this message in context: http://www.nabble.com/Return-valus-for-different-numbr-of-rows-tf4028854.html#a11446873 Sent from the R help mailing list archive at Nabble.com. __ R-help@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? [[alternative HTML version deleted]] __ R-help@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.
[R] loop in function
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@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.
Re: [R] loop in function
Actually I am not sure what you want exactly, but is it 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)) df1 - cbind(df1, colnames-(sapply(with(df2,(x+y)/z), function(a,b) a/b,b=df1$b), paste(goal,seq(nrow(df2)),sep=))) round(df1,2) b goal1 goal2 goal3 goal4 goal5 1 1 0.30 0.88 1.00 1.11 3.33 2 2 0.15 0.44 0.50 0.56 1.67 3 3 0.10 0.29 0.33 0.37 1.11 4 4 0.07 0.22 0.25 0.28 0.83 5 5 0.06 0.17 0.20 0.22 0.67 6 5 0.06 0.17 0.20 0.22 0.67 7 6 0.05 0.15 0.17 0.19 0.56 8 7 0.04 0.12 0.14 0.16 0.48 9 8 0.04 0.11 0.12 0.14 0.42 10 9 0.03 0.10 0.11 0.12 0.37 11 10 0.03 0.09 0.10 0.11 0.33 each column goal corresponds to row of df1. Alternatively, the sapply() function can be rewritten with apply(): apply(df2,1, function(a,b) (a[x]+a[y])/(a[z]*b), b=df1$b) Hope this answered your question... --- [EMAIL PROTECTED] 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@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. __ R-help@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.