Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good strategy in R to store results in list form to access it later on by names rather than indices?

Tags:

r

I would like to automate the results creation process when I apply certain modeling techniques. So I'll have different parameters which will be applied (e.g. hierarchical clustering distances and linkage methods). The results will have a matrix form so that I can access individual results by specifying the model parameters (eg. single, euclidean). In a dataframe I could name the columns and rows and access the elements simply by df[rname[1],cname[1]]. As far I as I read its not possible to use data frame objects to store list results. So I need lists for storing list-results. But in lists I can only specify lst$cname[1] and not both dimensions. Am I correct?

# data frame layout for numeric results does not work with list results
rname<-c("u","v","w")
cname<-c("ave","single")

# dataframe for results but does not work for results which are lists
paste.1<-function(x,y) paste(x,y,sep=".")
df1<-data.frame(lapply(cname,paste.1,x=rname),row.names=rname)
colnames(df1)<-cname

# creating list for results - do not get a good idea to proceed from here Advices??
lst<-(lapply(cname,paste.1,x=rname))
names(lst)<-cname

# results example - could be anything else 
# with a dataframe I could use df1[rname,cname]<-foo(rname,cname)
# with lists I guess its not as easy
require(graphics)
ave.u <- hclust(dist(USArrests,"euclidean"), cname[1])
ave.v <- hclust(dist(USArrests,"maximum"), cname[1])
ave.w <- hclust(dist(USArrests,"manhattan"), cname[1])
single.u <- hclust(dist(USArrests,"euclidean"), cname[2])
single.v <- hclust(dist(USArrests,"maximum"), cname[2])
single.w <- hclust(dist(USArrests,"manhattan"), cname[2])

Well I am not sure if there is a solution which I guess must exist. At the end I just want to access the list results via the row names and column names. I know I could transfer row/column names to numerical ones and then play with assigning proper indexes to find my results in a list of length(rname) x length(cname) but since the data frame is so nicely to use I am assuming it must be an easy way to store it more user friendly. It might also be the case that I did not really get well into the concept of lists since I am just starting to play around with R. So my question is: What would be a good strategy to store the structured results (which are lists) using R?

like image 432
Sebastian Avatar asked Mar 08 '11 17:03

Sebastian


People also ask

Can you store a list within a list in R?

The list data structure in R allows the user to store homogeneous (of the same type) or heterogeneous (of different types) R objects. Therefore, a list can contain objects of any type including lists themselves.

What is the list in R?

A list is an object in R Language which consists of heterogeneous elements. A list can even contain matrices, data frames, or functions as its elements. The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to access them.


1 Answers

One can have a matrix of lists:

nr <- length(rname)
nc <- length(cname)

m <- matrix(list(), nr, nc, dimnames = list(rname, cname))

m[["u", "ave"]] <- ave.u

# etc.

For example, form the row names, rnm, and column names, cnm, and a data frame, g, of all combinations of their values. Then create a matrix of lists, m :

rnm <- c("euclidean", "maximum", "manhattan")
cnm <- c("ave", "single")
g <- expand.grid(rnm, cnm)
f <- function(i) hclust(dist(USArrests, g[i,1]), g[i,2])
m <- matrix(lapply(1:nrow(g), f), length(rnm), dimnames = list(rnm, cnm))

We can access elements like this:

> m[["euclidean", "single"]]

Call:
hclust(d = dist(USArrests, g[i, 1]), method = g[i, 2])

Cluster method   : single 
Distance         : euclidean 
Number of objects: 50
like image 164
G. Grothendieck Avatar answered Sep 30 '22 11:09

G. Grothendieck