[R] Finding elements in a character vector

Downey, Patrick PDowney at urban.org
Sat Apr 9 02:05:44 CEST 2011


Perfect. Thank you.

-Mitch

-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu] 
Sent: Friday, April 08, 2011 8:03 PM
To: Downey, Patrick
Cc: r-help at r-project.org
Subject: Re: [R] Finding elements in a character vector

match(list.a,list.b)

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Fri, 8 Apr 2011, Downey, Patrick wrote:

> Hello,
>
> 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 things.
>
> for(i in 1:length(list.a)){
> index[i] <- seq(1,length(list.b))[list.b==list.a[i]]
> }
>
> Thank you,
> Mitch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list