[R] Finding elements in a character vector

Downey, Patrick PDowney at urban.org
Sat Apr 9 01:52:22 CEST 2011


I have two character vectors: list.a and list.b. Every element of list.a
appears somewhere in list.b. Not all elements of list.b are in list.a, and
some elements of list.b appear multiple times in list.a. I want to create a
new vector (index) of the same length as list.a where the nth element of
index is the location in list.b of the nth element of list.a. 

This code will work, but I have heard over and over again that using loops
is inefficient in R and that there are (almost always) better ways to do

for(i in 1:length(list.a)){
index[i] <- seq(1,length(list.b))[list.b==list.a[i]]

Thank you,

More information about the R-help mailing list