Viktor Plattner
Viktor Plattner

Reputation: 15

loop over multi level lists to plot their elements in R

I need your help in the following:

I have multi level lists (lists in lists) and I have to plot their elements on one figure. One plot is easy and it looks like this (sorry about the non-generalized variable names)

plot(powerList$ch1$frequency, powerList$ch1$power, type='hist', xlim = c(0,0.001))

powerList contains other lists (ch1...)

I have to plot frequency and power in lists ch1 to ch13.

I've tried the following:

created a vector with the channel names (second list)

chNames = c('ch1','ch2','ch3','ch4','ch7','ch8','ch9','ch10','ch11','ch13','ch14','ch15','ch16')

then I've tried to loop over it: first I created a 4 times 4 window

par(mfrow=c(4,4))

for (i in chNames){
  plot(powerList$i$frequency, powerList$i$power, type='hist', xlim = c(0,15))
}

par(mfrow=c(1,1))

I know that the code is wrong. It is just to show clearly what I want to do. Is there a way to do it simply and effectively?

Thank you

Upvotes: 0

Views: 410

Answers (1)

Daniel Anderson
Daniel Anderson

Reputation: 2424

Basically, you just need to loop through the list with indexing.

for (i in seq_along(powerList)) { 
    plot(powerList[[i]]$frequency, powerList[[i]]$power, type='hist', xlim = c(0,15)) 
}

Upvotes: 1

Related Questions