0

I have an array of dataframes, all with the same colums. I would like to automatically plot a different line for a specific column for each of this dataframes. Something like:

plot(array[]$time, array[]$data)

is something like that possible, or do I have to loop each dataframe and add a line() for each dataframe?

Edit

I beg your pardon, in fact what I created is a list. Basically I have two tables, connections that list different TCP conections informations:

src | src_port | dst | dst_port

and probes that contains timeseries data regarding packets and data transmitted:

timestamp | src | src_port | dst | dst_port | packets | bytes

So to plot the timeseries of all the different connections, I created a list of dataframe subsets, like that:

connection <- vector(mode='list', length = nrow(connections))
for (row in 1:nrow(connections)){
  connection[[row]] <- subset(probes, src == connections[row, 'src'] & src_port == connections[row, 'src_port'] & dst == connections[row, 'dst'] & dst_port == connections[row, 'dst_port'])
}

What I want to obtain is to plot all these subset having in the x axis the timestamp and in the y axis the bytes, considering a different timesries for each connection.

I hope I better clarified the problem now.

ale93p
  • 464
  • 8
  • 20
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do you really have an array of data.frames? or a list of data.frames? Those are different in R. Often it makes sense to merge your data to a single data.frame before plotting. – MrFlick Feb 21 '18 at 16:46
  • To my knowledge there is no such thing as array of data.frames in R. Maybe you mean list of data.frames? In this case you can use lapply(). – Aleh Feb 21 '18 at 17:04
  • @Aleh: Your knowledge of R is too limited. Dataframes are lists and lists can be indexable elements of matrices or arrays. – IRTFM Feb 21 '18 at 17:08
  • @42 might be, please provide an example of this in code. Otherwice as said you can store data.frames in list and run an lapply() to fill the plot. – Aleh Feb 21 '18 at 17:14
  • @Aleh I'm sorry, I just edited adding more details – ale93p Feb 21 '18 at 17:19

2 Answers2

2

Here's a reproducible example of plotting multiple dataframes extracted from a three-dimensional array. Notice the need to use "[[" to process the indices, and the fact that the default type of graphic for plot is points rather than lines. Could change that with type="l":

dfarray <- array( list(), c(2,3,4))
dfarray[[1,1,1]] <- data.frame(a=2:4, letters[2:4])  # need to use "[["
dfarray[[1,1,2]] <- data.frame(a=2:4, b=8:10)
dfarray[[1,1,3]] <- data.frame(a=2:4, b=10:12)
# Remember to make enough space to hold lines
 png(); plot(b ~a, data=dfarray[[1,1,1]], ylim=c(5,12) ) 
        for( x in 2:3) {lines( b~a, data=dfarray[[1,1,x]], col=x)}
 dev.off()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thank you, I didn't know I could use the relation y~x in plot. This solved my problem. – ale93p Feb 21 '18 at 17:32
  • 1
    There are multiple `plot` (and `lines`) "methods". They are known as "generic functions". This usage is described in `?plot.formula` – IRTFM Feb 21 '18 at 17:42
0

This was quite interesting. I think we can generalise the for loop like this:

lapply(X = c(dfarray), FUN = function(x) {lines(x = x$a, y = x$b,   ylim=c(5,12))}
Aleh
  • 776
  • 7
  • 11