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.
Tell the difference between characters
7 messages · Jorge Ivan Velez, Sarah Goslee, jim holtman +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110426/1f63d5e0/attachment.pl>
Hi Lisa,
On Tue, Apr 26, 2011 at 2:09 PM, Lisa <lisajca at gmail.com> wrote:
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.
Is this what you're looking for?
testchar <- function(x)
{
substring(x, 1, 1) == substring(x, 2, 2)
}
testchar("aa")
[1] TRUE
testchar("aB")
[1] FALSE
temp <- c("aa", "aA", "ab")
sapply(temp, testchar)
aa aA ab TRUE FALSE FALSE
Sarah Goslee http://www.functionaldiversity.org
Yes. That is what I want. Thank you very much. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Tell-the-difference-between-characters-tp3476130p3476288.html Sent from the R help mailing list archive at Nabble.com.
This will handle varying length strings if you want to test for the same character:
temp <- c("aa", "aA", "ab")
x <- strsplit(temp, '')
x
[[1]] [1] "a" "a" [[2]] [1] "a" "A" [[3]] [1] "a" "b"
sapply(x, function(z) all(z[1] == z))
[1] TRUE FALSE FALSE
On Tue, Apr 26, 2011 at 2:09 PM, Lisa <lisajca at gmail.com> wrote:
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.
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Thanks a lot. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Tell-the-difference-between-characters-tp3476130p3476352.html Sent from the R help mailing list archive at Nabble.com.
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:
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.
______________________________________________ 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.
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O