0

I've got a list called res that looks like this:

[[1]]
     [,1]     [,2]    
[1,] 275.0637 273.9386
[2,] 5.707791 5.755798

[[2]]
     [,1]     [,2]    
[1,] 126.8435 59.08806
[2,] 4.867521 3.258545

[[3]]
     [,1]     [,2]    
[1,] 23.50188 60.96321
[2,] 2.036354 3.737291

The list contains results from a simulation run a total of 6 times. I set a parameter of interest at three different values, '0' (ie., [[1]]), '25' (i.e.,[[2]]), and '50' (i.e.,[[3]]). Since the model includes a great deal of randomness I ran the model twice for each value (i.e., [,1], [,2]). I asked the model to record two results, 'time feeding' (i.e., [1,] and 'distance traveled' (i.e., [2,]) for each iteration. Ultimately I will iterate the model 30 times for each variable setting. I'd like to use ggplot to create a boxplot showing 'time feeding' and 'distance traveled' for each of the three simulation settings (i.e., 0,25,50). I believe ggplot can't plot a list so I tried to convert res to a dataframe using res2 <- data.frame(res) which looked like:

        X1       X2     X1.1     X2.1     X1.2     X2.2
1 275.0637 273.9386 126.8435 59.08806 23.50188 60.96321
2 5.707791 5.755798 4.867521 3.258545 2.036354 3.737291

This doesn't quite look right to me because now the results from all three simulations are on the same row. Any help on bringing this data into ggplot to create a boxplot with would be really helpful. Thanks in advance!

--Neil

2 Answers 2

1

Assuming ll is your list , you can use do.call and rbind like this :

do.call(rbind,lapply(seq_along(ll),
        function(x)data.frame(ll[[x]],iter=x)))

           X..1.      X..2. iter
[1,]  275.063700 273.938600    1
[2,]    5.707791   5.755798    1
[1,]1 126.843500  59.088060    2
[2,]1   4.867521   3.258545    2
[1,]2  23.501880  60.963210    3
[2,]2   2.036354   3.737291    3

EDIT after op clarication:

interest <- c(0,25,50)
 do.call(rbind,lapply(seq_along(ll),
        function(x)data.frame(x= unlist(ll[[x]]),interst=interest[x])))

                                               interst=interest[x] .... [TRUNCATED] 

                 x interst
X..1.1  275.063700       0
X..1.2    5.707791       0
X..2.1  273.938600       0
X..2.2    5.755798       0
X..1.11 126.843500      25
X..1.21   4.867521      25
X..2.11  59.088060      25
X..2.21   3.258545      25
X..1.12  23.501880      50
X..1.22   2.036354      50
X..2.12  60.963210      50
X..2.22   3.737291      50

EDIT since OP don't provide data here ll :

res <- list(read.table(text='
     [,1]     [,2]    
[1,] 275.0637 273.9386
[2,] 5.707791 5.755798'),
read.table(text='
     [,1]     [,2]    
[1,] 126.8435 59.08806
[2,] 4.867521 3.258545'),
read.table(text='
     [,1]     [,2]    
[1,] 23.50188 60.96321
[2,] 2.036354 3.737291'))
Sign up to request clarification or add additional context in comments.

3 Comments

That's great! Although is it possible to have the values in the two columns (e.g., 275.06, 273.93) be listed in a single column? That way I can ask ggplot to ggplot(res, aes(x=iter, y=X1)) + geom_boxplot())
Thanks again! Can the 'x' column be split into two columns such that 'time feeding' (i.e., 275, 273, 126, etc) and 'distance traveled' (i.e., 5.7,5.75,4.86,etc.) are separate? Otherwise, I'm not sure how ggplot can recognize which variable to plot.
@user2359494 Try to understand my answer ! I will not do the job for you. Take the transpose and you get what you want: do.call(rbind,lapply(seq_along(ll),function(x)data.frame(x= t((ll[[x]])), interst=interest[x])))
0

I would do

names(res) = c("0", "25", "50")
m = reshape2::melt(res, id = 1)

but maybe it doesn't work, I tried it in my head because you didn't provide data in usable form.

3 Comments

it's unlikely to be the same type of data
Why? for me it is a list of 3 data.frames..?
from the display, they're probably matrices, initially

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.