[R] for loop

Joshua Wiley jwiley.psych at gmail.com
Fri Oct 15 00:37:14 CEST 2010


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/



More information about the R-help mailing list