I am having some problems with R and nested lists. I want to have a dictionary like structure (or array).
I have 12 categories (1-12), and each of them has a list of items. I have a table with all my data and want to split it, to show it in different UI components. The table has x rows, and one column. The rowname is my item, the column value is my category.
Table:
rowname | value
"A" | 3
"B" | 1
"C" | 2
"D" | 3
"E" | 2
What I want as result:
data = { [1] = ("B"); [2] = ("C", "E"); [3] = ("A", "D") }
What I tried:
#init list of empty lists
data <- list()
for(i in 1:12){
data[i] <- list()
}
for(i in 1:nrow(myTable)){
val <- myTable[i, 1]
name <- rownames(myTable)[i];
print(paste(val, "-", name))
tmp <- data[val]
tmp[[name]] <- name
data[val] <- tmp
}
print(data)
What is the best way to achieve this. This is my first time using R, so I might be missunderstanding basic language constructs.