Reputation: 197
I have a long list of repeating strings- here is some toy data,
unique_items <- c('shoes', 'shirt', 'pants', 'socks')
l <- unique_items[sample(1:4, 100000, replace = TRUE)]
I would now like to find ALL of the indices of each of the items in unique_items.
A naive solution would be:
item_indices <- vector(mode = 'list', length = length(unique_items))
names(item_indices) <- unique_items
for (idx in 1:length(unique_items))
{
item <- unique_items[idx]
item_indices[[item]] <- which(l == item)
}
However, that is incredibly slow when I have a huge list.
Is there a faster way to do this?
Upvotes: 3
Views: 229