[R] Improve a browse through list items - Transform a loop to apply-able function.
Patrizio Frederic
frederic.patrizio at gmail.com
Wed Dec 14 11:22:30 CET 2011
Hi robin,
I'm not sure is what you need, but that's an esthetically nice
solution (one single line without any loop :) )
matrix(apply(log(cbind(as.numeric(a),as.numeric(b),as.numeric(c),as.numeric(d))),1,sd),3)
hope it could help,
PF
On Mon, Dec 12, 2011 at 5:15 PM, Robin Cura <robin.cura at gmail.com> wrote:
> Hello,
>
> I'm currently trying to convert a slow and ugly script I made, so that it's
> faster and can be computed on a computer grid with the multicore package.
> My problem is that I don't see how to turn some loops into an "apply-able"
> function.
>
> Here's an example of my loops :
> I got a list of dataframes (or matrices like here), and I need to browse
> each cell of those many dataframes to compute a mean (or standard deviation
> like here).
>
> Here's a example script :
>
> a <- b <- c <- d <- result <- matrix(nrow=3, ncol=3)
> a[] <- sample.int(n=100,size=9,replace=TRUE)
> b[] <- sample.int(n=100,size=9,replace=TRUE)
> c[] <- sample.int(n=100,size=9,replace=TRUE)
> d[] <- sample.int(n=100,size=9,replace=TRUE)
> result[] <- NA
> mylist <- list(a,b,c,d)
>
> for (row in 1:3)
> {
> for (col in 1:3)
> {
> tmpList <- log(mylist[[1]][row, col])
> for (listitem in 2:4)
> {
> tmpList <- c(tmpList, log(mylist[[listitem]][row, col]))
> }
> result[row, col] <- sd(tmpList)
> }
> }
>
> Considering I have to look at the same cell in each dataframe, I don't
> understand how I could turn this into a function, considering I need the
> row and column number to iterate.
>
> I succeeded improving my script duration a lot, but such loops are really
> long to run, considering that my lists contains like 100 dataframes, who
> all contains thousands of values.
>
> Any help would be really appreciated
>
> Thanks in advance,
>
> Robin
>
> [[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.
--
+-----------------------------------------------------------------------
| Patrizio Frederic,
| http://www.economia.unimore.it/frederic_patrizio/
+-----------------------------------------------------------------------
More information about the R-help
mailing list