This is a follow-up of this question, as I have trouble extracting the data that I need using the Filter and lapply functions. Let's say I have the following data (which is of course only an excerpt):
result <- list(
list(
startSize = 2, method = "pa",
list(time = 0.0016831, pk = c(1, 0), ij = c(10, 0, 0, 0, 0, 0)),
list(time = 0.0002714, pk = c(1.1), ij = c(0)),
list(time = 5.26e-05, pk = c(), ij = c())
),
list(
startSize = 2, method = "pa",
list(time = 0.0004191, pk = c(2, 0), ij = c(22, 0, 0, 0, 0, 0)),
list(time = 0.0002401, pk = c(0.5, 1.2), ij = c(0)),
list(time = 5.28e-05, pk = c(), ij = c())
),
list(
startSize = 4, method = "pnb",
list(time = 0.0009156, pk = c(3, 0, 0, 0), ij = c(33, 0, 0, 0,0, 0)),
list(time = 0.0010072, pk = c(4, 0, 0, 0), ij = c(44, 0, 0, 0.25, 0, 0, 0, 0, 0, 0.25, 0, 0, 0, 0, 0, 0)),
list(time = 0.0005579, pk = c(5, 0), ij = c(55, 0.0625, 0.0625, 0.0625, 0.0625, 0))
),
list(
startSize = 4, method = "pnb",
list(time = 0.0009364, pk = c(6, 0, 0, 0), ij = c(66, 0, 0, 0, 0, 0)),
list(time = 0.0023319, pk = c(7, 0, 0, 0), ij = c(77, 0, 0, 0, 0.25, 0, 0, 0.25, 0, 0, 0.33, 0.25, 0, 0, 0, 0, 0, 0)),
list(time = 0.0013074, pk = c(8, 0), ij = c(8.8, 0.0625, 0.0625, 0.0625, 0.0625, 0))
)
)
As you can see, I have a list containing 4 nested lists; I have 2 different startSizes (2/4), 2 different methods (pa, pnb) and each experiment was run 2 times. Each experiment contains 3 iterations and the time is recorded, together with 2 vectors pk and ij.
Based on @r2evens answer in the other question, I was hoping to
- write functions to make filtering easier, for example something like
myFilterFunction1 <- function(mylist, size, meth) {
return(mylist["startSize"] == size && mylist["method"] == meth)
}
or
myFilterFunction2 <- function(mylist, size, meth) {
return Filter(function(z) z["startSize"] == size && z["method"] == meth, mylist)
}
which I can then use to further filter the date using lapply. Both these two functions give me an error ("'list' object cannot be coerced to type 'double'"), so at this point I haven't even gotten to play around with lapply. :(
Here is the kind of output that I would like extract:
- times: For
startSize=2,method="pa", get matrix
| times | ,1 | ,2 | ,3 |
|---|---|---|---|
| 1, | 0.0016831 | 0.0002714 | 5.26e-05 |
| 2, | 0.0004191 | 0.0002401 | 5.28e-05 |
- vector lengths: For vector
"pk",startSize=2,method="pa", get matrix
| length(pk) | ,1 | ,2 | ,3 |
|---|---|---|---|
| 1, | 2 | 1 | 0 |
| 2, | 2 | 2 | 0 |
- combined vector (so I can compare boxplots, means, etc.) by iteration. For vector
"pk",startSize=2,method="pa", get vectors
c(1, 0, 2, 0), c(1.1, 0.5, 1.2), c()
I feel like I'm so close, yet I could not get it to work the past 7 hours, so I would be very thankful for some help!
Thank you in advance!