[R] matching-case sensitivity

Marc Schwartz MSchwartz at medanalytics.com
Tue Aug 26 22:42:42 CEST 2003


On Tue, 2003-08-26 at 15:09, Jablonsky, Nikita wrote:
> Hi All,
> 
> I am trying to match two character arrays (email lists) using either
> pmatch(), match() or charmatch() functions. However the function is
> "missing" some matches due to differences in the cases of some letters
> between the two arrays. Is there any way to disable case sensitivity or is
> there an entirely better way to match two character arrays that have
> identical entries but written in different case?
> 
> Thanks
> Nikita


At least two options for case insensitive matching:

1. use grep(), which has an 'ignore.case' argument that you can set to
TRUE. See ?grep

2. use the function toupper() to convert both character vectors to all
upper case. See ?toupper.  Conversely, tolower() would do the opposite.


A quick solution using the second option would be:

Vector1[toupper(Vector1) %in% toupper(Vector2)]

which would return the elements that match in both vectors.


A more formal example with some data:

Vector1 <- letters[1:10]
Vector1
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"


Vector2 <- c(toupper(letters[5:8]), letters[9:15])
Vector2
[1] "E" "F" "G" "H" "i" "j" "k" "l" "m" "n" "o"


Vector1[toupper(Vector1) %in% toupper(Vector2)]
[1] "e" "f" "g" "h" "i" "j"


HTH,

Marc Schwartz




More information about the R-help mailing list