Skip to content

Tell the difference between characters

7 messages · Jorge Ivan Velez, Sarah Goslee, jim holtman +2 more

#
Dear all,

I just want to determine if the characters in a character string are the
same or not. For example,

temp <- c("aa", "aA", "ab")

How do I determine the first one have the two same ?a?, and the second and
third have the different characters? Thanks in advance.

Lisa
 


--
View this message in context: http://r.789695.n4.nabble.com/Tell-the-difference-between-characters-tp3476130p3476130.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi Lisa,
On Tue, Apr 26, 2011 at 2:09 PM, Lisa <lisajca at gmail.com> wrote:
Is this what you're looking for?

testchar <- function(x)
{
substring(x, 1, 1) == substring(x, 2, 2)
}
[1] TRUE
[1] FALSE
aa    aA    ab
 TRUE FALSE FALSE
#
This will handle varying length strings if you want to test for the
same character:
[[1]]
[1] "a" "a"

[[2]]
[1] "a" "A"

[[3]]
[1] "a" "b"
[1]  TRUE FALSE FALSE

        
On Tue, Apr 26, 2011 at 2:09 PM, Lisa <lisajca at gmail.com> wrote:

  
    
#
Try this:

sapply(apply(sapply(temp, charToRaw), 2, unique), length) == 1
On Tue, Apr 26, 2011 at 3:09 PM, Lisa <lisajca at gmail.com> wrote: