BoTz
BoTz

Reputation: 492

data structure with vectors as elements in R

I need to plot a large number of graphs from the same data frame. each plot describes a different group of variables in the data frame, but each variable is contained in several plots. My plan is to loop through a data structure which would include the name for each graph, and the vector of variables indexes to plot.

for example:

plot names range other parameters
ICC - 1 c(1:20) 1
IIC - 2 c(10:30) 2
TCC - 1b c(20:40) 1
TIC - 2x c(20:40) 4

However, I can't find a suitable data structure in R, in which one column would contain the list of plot names (strings) and the second column would be a list of variable ranges (vectors). additional column might contain other parameters for each plot.

So,

  1. Is there a suitable data structure in R?
  2. Is there a better way to go about it?

Thanks!

Upvotes: 1

Views: 73

Answers (1)

r2evans
r2evans

Reputation: 161085

What you want is list-columns. It's a little difficult to build them from the start, but not so hard to add them later.

### won't work
dat <- data.frame(a=c("ICC-1","IIC-2"), range=list(1:10, 10:30))
# Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
#   arguments imply differing number of rows: 10, 21

### this does work
dat <- data.frame(a=c("ICC-1","IIC-2"))
dat$range <- list(1:10, 10:30)
dat
#       a                                                                              range
# 1 ICC-1                                                      1, 2, 3, 4, 5, 6, 7, 8, 9, 10
# 2 IIC-2 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30

(It is feasible to add 1:10 as a quoted expression if you'd prefer, but that takes more care in follow-on processing that I did not want to assume.)

Upvotes: 4

Related Questions