I am trying to implement another Hierarchical Clustering algorithm and I thought that I can use MANY nested lists. This is my start list
mdata <- list(node = list(n1 = list(cluster = c(1,2,3), sth = "junk")))
I can use lapply to access first node and produce this:
mdata_itr1 <- lapply(mdata$node, function(x){
node <- list(node = list(n1 = list(cluster = c(3,2,1), sth = "anything",
n2 = list(cluster = c(2,3,1), sth = "anything"))))
x <- append(x, node)})
It should be like a binary tree. Each node consists of two nodes (n1, n2) with information like cluster, sth, then another node. I want to have as many nodes as I want. I was trying to play with a recursive function, but honestly, it's my first time with them. I kinda get the concept, but for such a task, it is currently getting way above my head.
addNode <- function(mdata, itr = 3){
out <- lapply(mdata$node, function(x, itr){
node <- list(node = list(n1 = list(cluster = paste0("I was here. Thx. ", itr), sth = "junk"),
n2 = list(cluster = paste0("I was here too ", itr), sth = "junk")))
x <- append(x, node)
itr = itr - 1
if(itr==0){
return(x)
}else{
x <- append(x, addNode(x, itr))
}
}, itr = itr)
return(out)}
It is just one of many attempts. I was trying to play with e1 <- environment() and e1$mdata <- gdata::update.list(object = get("mdata", e1), new = node). But as before, it was too much for me.
Do you have any ideas? Or is there a better way to store this amount of information in a tree-like format? I can do all this just by writing files to the disk in an ordered manner, but I am looking for a better solution. I am kindly asking for any advice!
I am adding the expected result, as output from dput()
list(node = list(n1 = list(cluster = c(1, 2, 3), other_info = "other",
node = list(n1 = list(cluster = c(1, 2, 3), other_info = "other"),
n2 = list(cluster = c(1, 2, 3), other_info = "other"))),
n2 = list(cluster = c(1, 2, 3), other_info = "other", node = list(
n1 = list(cluster = c(1, 2, 3), other_info = "other"),
n2 = list(cluster = c(1, 2, 3), other_info = "other")))))