Dear all, I have a function f(x) which return a list as result. $T1 [1] 0.03376190 $T2 [1] 0.04725 $T3 [1] 0.3796071 $T4 [1] 0.3713452 $T5 [1] 0.4523651 $T6 [1] 0.4575873 I now find the result for a vector of x values at one time. I want to store the reuslt for each xi value in a column of a matrix x <- seq(0,1, by=0.1) result <- matrix(0, nrow=6, ncol=length(x)) for (i in 1:length(x)){result[,i] <- f(x[i])} It is not working. Can some help me. [[alternative HTML version deleted]]
you probably want to use 'unlist'; can not test since you did not provide a reproducible script: for (i in 1:length(x)){result[,i] <- unlist(f(x[i]))} On Thu, Oct 14, 2010 at 5:50 PM, li li <hannah.hlx at gmail.com> wrote:> Dear all, > ?I have a function f(x) ?which return a list as result. > > $T1 > [1] 0.03376190 > $T2 > [1] 0.04725 > $T3 > [1] 0.3796071 > $T4 > [1] 0.3713452 > $T5 > [1] 0.4523651 > $T6 > [1] 0.4575873 > > ?I now find the result for a vector of x values at one time. I want to > store the reuslt > for each xi value in a column of a matrix > > ?x <- seq(0,1, by=0.1) > result <- matrix(0, nrow=6, ncol=length(x)) > > for (i in 1:length(x)){result[,i] <- f(x[i])} > > It is not working. Can some help me. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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 that you are trying to solve?
Here's one way to do it: Use unlist() to change the function's output from a list to a vector.> f <- function(x) {return(lapply(1:6, function(z) z + x))} > f(2)[[1]] [1] 3 [[2]] [1] 4 [[3]] [1] 5 [[4]] [1] 6 [[5]] [1] 7 [[6]] [1] 8> x <- seq(0,1, by=0.1) > result <- matrix(0, nrow=6, ncol=length(x)) > for (i in 1:length(x)){result[,i] <- unlist(f(x[i]))} > result[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 [2,] 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 [3,] 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 [4,] 4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 [5,] 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6 [6,] 6 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7>HTH Steven McKinney ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of li li [hannah.hlx at gmail.com] Sent: October 14, 2010 2:50 PM To: r-help Subject: [R] help Dear all, I have a function f(x) which return a list as result. $T1 [1] 0.03376190 $T2 [1] 0.04725 $T3 [1] 0.3796071 $T4 [1] 0.3713452 $T5 [1] 0.4523651 $T6 [1] 0.4575873 I now find the result for a vector of x values at one time. I want to store the reuslt for each xi value in a column of a matrix x <- seq(0,1, by=0.1) result <- matrix(0, nrow=6, ncol=length(x)) for (i in 1:length(x)){result[,i] <- f(x[i])} It is not working. Can some help me. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.
Dears R I am a self taught novice user of R. I begin to understand its philosophy and some basics like objects, vectors, arrays, lists etc...I still am not able to manipulate variables (objects!) in dataframes (objects also !!!) already imported from my usual statistical package (stata 10/SE) despite carefull lecture of some good books (R Introduction, R Book of Crowley etc...). How must I proceed in order to go over this ostacle? Thanks and regards Kambale Mastaki, MD Falconara M (Italy) Phone: 0039/3496331454 PS: A reaction in in French language would be particularly appreciated. [[alternative HTML version deleted]]
Hello On Sat, Oct 23, 2010 at 10:43 AM, Mastaki Kambale <jkmastaki at hotmail.com> wrote:> > Dears R > I am a self taught novice user of R. I begin to understand its philosophy and some basics like objects, vectors, arrays, lists etc...I still am not able to manipulate variables (objects!) in dataframes (objects also !!!) already imported from my usual statistical package (stata 10/SE) despite carefull lecture of some good books (R Introduction, R Book of Crowley etc...). How must I proceed in order to go over this ostacle? >If I understand correctly, you have trouble importing Stata files. Have you tried library(Rcmdr), Data > Import > Stata ? I think that JGR and Rattle also provide some graphical facilities for this. Regards Liviu> Thanks and regards > Kambale Mastaki, MD > Falconara M (Italy) > Phone: 0039/3496331454 > > PS: A reaction in in French language would be particularly appreciated. > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >-- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail