0

I am using the following code to run simulations. How can I get a from x1? I have tried lapply below but doesn't seem to work.

library(parallel)
clusterEvalQ(cl,library(evir))
set.seed(0)
system.time(
 x1 <- parLapply(cl, 1:100000,
   function(i) {
       n1 <- rpois(1,4)
       n2<-  rpois(1,7)
       list(data.frame(a=rexp(n1, rate=0.1),a1=rexp(n1, rate=0.6)), 
            data.frame(b=rexp(n2, rate=0.7),b1=rexp(n2, rate=0.6)))

   }
  )
)

y1<-lapply(x1, '[', , "a")
3
  • Can we assume that the number of a data frame (in the case of a or a1 - 1) is known? Or should we look for this column across all data frames? Commented Mar 28, 2016 at 15:54
  • @Julius we can assume it is known Commented Mar 28, 2016 at 16:16
  • a is not a data.frame it's a vector within a data.frame. The same is true of b... You don't understand the data structures you're working with. Commented Mar 28, 2016 at 17:04

1 Answer 1

1
y1 <- lapply(x1, function(x) x[[1]][["a"]])

gives a required list of vector assuming that the number of a data frame is known and equals 1. More generally:

y1 <- Filter(Negate(is.null), sapply(unlist(x1, rec = FALSE), `[[`, "a"))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i slightly changed the code above. How can I get only a or a1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.