1D data classification 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 &lt;- function(names) {
    mylist.names &lt;- names
    mylist &lt;- vector("list", length(mylist.names))
    names(mylist) &lt;- mylist.names
    return(mylist)
}


#Random integers
rData &lt;- sample(x = as.integer(c(1:100)), size = 100)

#Random class  
rClass &lt;- sample(x = c(1:10), size = 100, replace = TRUE)

#Separate rData according to his class in rClass
i &lt;- 0
ClassifiedrData &lt;- CreateEmptyDictionary(names &lt;- sort(unique(rClass)))

for (a in rData) {
    i &lt;- i + 1
    #ClassifiedrData[[rClass[i]]] &lt;-  append(ClassifiedrData[[rClass[i]]], a)
    ClassifiedrData[[rClass[i]]][length(ClassifiedrData[[rClass[i]]])+1] &lt;- a
}
This is actually a 1-liner with the built-in split function:

ClassifiedrDataSplit &lt;- split(rData, rClass)


You can confirm this returns the same result as your original code:

identical(ClassifiedrData, ClassifiedrDataSplit)
# [1] TRUE


In addition to being a lot less code, this will result in sensible speedups for larger datasets. For instance, consider the performance with 10 million elements and 10 classes:

rData &lt;- sample(x = as.integer(c(1:1e7)), size = 1e7)
rClass &lt;- sample(x = c(1:10), size = 1e7, replace = TRUE)
system.time({i &lt;- 0
ClassifiedrData &lt;- CreateEmptyDictionary(names &lt;- sort(unique(rClass)))

for (a in rData) {
    i &lt;- i + 1
    #ClassifiedrData[[rClass[i]]] &lt;-  append(ClassifiedrData[[rClass[i]]], a)
    ClassifiedrData[[rClass[i]]][length(ClassifiedrData[[rClass[i]]])+1] &lt;- a
}})
#    user  system elapsed 
#   5.248   0.435   5.749 
system.time(split(rData, rClass))
#    user  system elapsed 
#   0.258   0.083   0.357 


The solution with split is about 10x faster.