[R] code efficiency, extr. info from list
Anders Nielsen
anielsen at math.ku.dk
Tue Oct 21 17:51:37 CEST 2003
Try using lapply()
For instance like:
val<-unlist(lapply(test, function(x)x$value))
You can also extend this by having your function return
everything you need from the list.
Cheers,
Anders.
On Tue, 21 Oct 2003, Tord Snall wrote:
> Dear all,
> I try extracting information from a list with several levels, but I would
> be happy for recommendation on writing more efficient code:
>
> > h0<- seq(0,100, by = 20); expo<- seq(0.1, 0.5, l = 5)
> > grid<- expand.grid(h0, expo)
> > test<- apply(grid, 1, pcp, point.data = as.points(dat[,c("x","y")]),
> poly.data = studyarea)
>
> > test[1]
> $"1"
> $"1"$par
> s2 rho
> 1.815343e-06 2.358788e-02
>
> $"1"$value
> [1] 144.346
>
> $"1"$counts
> function gradient
> 65 NA
>
> $"1"$convergence
> [1] 0
>
> $"1"$message
> NULL
>
> I want to put the results together:
> val<- c(test[[1]]$value, test[[2]]$value, test[[3]]$value, test[[4]]$value...)
> s2<- c(test[[1]]$par[1], test[[2]]$par[1], test[[3]]$par[1],
> test[[4]]$par[1]...)
> rho<- ...
> funct<- ....
> grad<-
> .
>
> useful.df<- as.data.frame(cbind(val, s2....), F)
>
> However, as you can see
> > dim(grid)
> [1] 30 2
>
> the call rows
>
> val<- c(test[[1]]$value, test[[2]]$value, test[[3]]$value,
> test[[4]]$value.......)
> etc.
>
> will be long.
>
> I would thus be happy for help with writing this code more efficient (and I
> know will benefit from this knowing how to do this in the future).
>
>
> Thanks in advance!
>
> Sincerely,
> Tord
>
> -----------------------------------------------------------------------
> Tord Snäll
> Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
> Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
> Villavägen 14
> SE-752 36 Uppsala, Sweden
> Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
> Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
> Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
> E-mail: Tord.Snall at ebc.uu.se
> Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
More information about the R-help
mailing list