wolfsatthedoor
wolfsatthedoor

Reputation: 7313

Vectorized version of which in R?

I want to run this loop:

   householdnums=numeric()
    for(i in 1:length(households)){
        hh = households[i]
        householdnums = c(householdnums,which(hhlist==hh))
    } 

Where households and hhlist is a large vector. The problem is that households has duplicates of elements in hhlist, and I need to preserve the ordering. For example.

hhlist = c(1,2,3,4,5,6, 8)

households = c(1,2,1,1,4,5,6,8)

householdnums should be:

householdnums
[1] 1 2 1 1 4 6 6 7

Upvotes: 0

Views: 587

Answers (1)

wolfsatthedoor
wolfsatthedoor

Reputation: 7313

match : eg match(households, hhlist)

Upvotes: 1

Related Questions