This R code separates the values in rData
. They are classified according to the class in rClass
.
To do that, ClassifiedrData
is a named list, where each element in the list (a class), should be a vector with all the elements in that class.
It is slow, and I had read that I should not use for
in R, but I have no clue on how to vectorize it.
(I do not necessarily need a dictionary, so I welcome another data structure to store the classified numbers if it is more practical.)
I made random data (rData
and rClass
) as an example, but my real data is not random (so I simply creating the data classified is not a solution)
CreateEmptyDictionary <- function(names) {
mylist.names <- names
mylist <- vector("list", length(mylist.names))
names(mylist) <- mylist.names
return(mylist)
}
#Random integers
rData <- sample(x = as.integer(c(1:100)), size = 100)
#Random class
rClass <- sample(x = c(1:10), size = 100, replace = TRUE)
#Separate rData according to his class in rClass
i <- 0
ClassifiedrData <- CreateEmptyDictionary(names <- sort(unique(rClass)))
for (a in rData) {
i <- i + 1
#ClassifiedrData[[rClass[i]]] <- append(ClassifiedrData[[rClass[i]]], a)
ClassifiedrData[[rClass[i]]][length(ClassifiedrData[[rClass[i]]])+1] <- a
}