[R] vector operation using regexpr?

markleeds at verizon.net markleeds at verizon.net
Thu Aug 21 05:21:39 CEST 2008


Hi: I think you want regexpr so below does what you want but it doesn't 
handle the case when L isn't in the second column. I'm still trying to 
figure that out but don't count on it. Hopefully someone else will reply 
with that piece.

DF <- data.frame(col1="L",col2="MAIL",col3="PLOY")
print(DF)
index <- regexpr(DF$col1,DF$col2)
result <- substr(DF$col3,index,index)



On Wed, Aug 20, 2008 at  3:26 PM, John Christie wrote:

> Hi,
>
> Here's my problem... I have a data frame with three columns containing 
> strings.  The first columns is a simple character. I want to get the 
> index of that character in the second column and use it to extract the 
> item from the third column.  I can do this using a scalar method.  But 
> I'm not finding a vector method.  An example is below.
>
> col1      col2      col3
> 'L'         'MAIL '   'PLOY'
>
> What I want to do with the above is find the index of col1 in col2 (4) 
> and then use it to extract the character from col3 ('Y').  I could do 
> the last part if I could get the index in a vector fashion.
>
> So, the shorter question is, how do I get the index of the letter in 
> col1 as it is found in col2?
>
> ______________________________________________
> 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