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. Thank you very much! Hannah [[alternative HTML version deleted]]
Dear Hannah, Well one issue is that you are trying to assign a list to a matrix column. This is a hazardous move, and may well be your problem. Try: lapply(x, f) that should give you some nice results, and can probably be fairly easily converted to a matrix if you want. It is really hard to give you more help without having f() and without the exact details of what went wrong or the error message (if there was one). Best regards, Josh On Thu, Oct 14, 2010 at 2:53 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. > Thank you very much! > ? ? ? ? ? ? ? ? ? ? ? ?Hannah > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On 10/14/2010 2:53 PM, li li 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. > Thank you very much! > HannahIn order to test my solution, I needed a function that returned something of the structure you had. f <- function(x) { r <- as.list(rnorm(6)) names(r) <- paste("T",1:6,sep="") r } Using that, you can replace the for loop with: for (i in 1:length(x)){result[,i] <- unlist(f(x[i]))} The problem is that f returns a list; you can only put a vector in part of a matrix. unlist() takes care of that conversion. -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University
? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101030/7d59e415/attachment.pl>
Hi, I know this is probalby a very trivial thing to do for most of the R users, but since I just strated using it I have some problems.... I have a data.frame with a field called "razred". This field has values from 1 up to 15. Is it possible to create a for loop that would create a new data frame for each of the "razred" values. Thanks for the help, m [[alternative HTML version deleted]]
On Oct 30, 2010, at 2:07 PM, Matev? Pavli? wrote:> Hi, > > I know this is probalby a very trivial thing to do for most of the R > users, but since I just strated using it I have some problems.... > > I have a data.frame with a field called "razred". This field has > values from 1 up to 15. > > Is it possible to create a for loop that would create a new data > frame for each of the "razred" values.The R-way would be to use the split function and leave the result in a list to which the same operation could be also repeatedly performed using lapply. ?split And take a look at the fourth example applying split to the builtin airquality dataframe. The plyr package also provides functions on dataframes. -- David Winsemius, MD West Hartford, CT
Thanks for the suggestions. I add some more detail to clarify: h=matrix(nrow=1,ncol=22) In my data h is: (0.25 0.25 0 0 0 0 -0.25 -0.25 -0.25 -0.25 -0.5 -0.5 0 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25) xx<-seq(0,1,0.00005) v=matrix(nrow=20001,ncol=22) vv=matrix(nrow=20001,ncol=22) for (y in 1:20001) { v[y,22]=h[1,22] } vv[20001,22]=v[20001,22] for(k in 21:1) { for(j in 20001:2) { vv[j-1,k+1]=min(xx[j-1]*v[j-1,k+1],vv[j,k+1]) v[j,k]=h[1,k]+vv[j-1,k+1] } vv[20001,k]=v[20001,k] } The idea of using Rcpp seems to be good. Having never used this package will give a look. Somewhere I read an example like this: for (i in 1:R) { res[i]<-f() NULL } where f() is a function, but I don't how can I trasform my code: vv[j-1,k+1]=min(xx[j-1]*v[j-1,k+1],vv[j,k+1]) v[j,k]=h[1,k]+vv[j-1,k+1] in a function [[alternative HTML version deleted]]