Reputation: 733
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
Upvotes: 0
Views: 961
Reputation: 121608
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'))
Upvotes: 1
Reputation: 77114
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.
Upvotes: 0