David Andel
2003-Jul-17 22:07 UTC
[R] how to divide a string into characters? - for comparing strings that is
Hi I am searching for a way to do something like "ABC" -> c("A","B","C"). How can this be accomplished? I tried cut() and split(), but they do something else, it seems. The purpose for doing this is to find the number of common (and uncommon) characters, i.e. ultimately I want something like this:> foo("ABD","ADE")c(2,1) # 2 in x are in y, 1 in y is not in x> foo("AB","ADE")c(1,2) # 1 in x is in y, 2 in y are not in x Maybe I even do not need the string splitting? I hope I was clear in stating my problem. Thank you for your valuable input, David
Roger D. Peng
2003-Jul-17 22:21 UTC
[R] how to divide a string into characters? - for comparing strings that is
David Andel wrote:>Hi > >I am searching for a way to do something like "ABC" -> c("A","B","C"). How can this be accomplished? > >Try strsplit("ABC", "")[[1]]>I tried cut() and split(), but they do something else, it seems. > >The purpose for doing this is to find the number of common (and uncommon) characters, i.e. ultimately I want something like this: > > > >>foo("ABD","ADE") >> >> >c(2,1) # 2 in x are in y, 1 in y is not in x > > >>foo("AB","ADE") >> >> >c(1,2) # 1 in x is in y, 2 in y are not in x > >Maybe I even do not need the string splitting? > >I hope I was clear in stating my problem. > >Thank you for your valuable input, >David > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > >
Jerome Asselin
2003-Jul-17 22:29 UTC
[R] how to divide a string into characters? - for comparing strings that is
Here is a way of doing by splitting the character strings. x <- "ABD" y <- "ADE" x <- unlist(strsplit(x,"")) y <- unlist(strsplit(y,"")) c(sum(as.logical(match(x,y,nomatch=0))), sum(!as.logical(match(y,x,nomatch=0)))) HTH, Jerome On July 17, 2003 03:07 pm, David Andel wrote:> Hi > > I am searching for a way to do something like "ABC" -> c("A","B","C"). > How can this be accomplished? > > I tried cut() and split(), but they do something else, it seems. > > The purpose for doing this is to find the number of common (anduncommon) characters, i.e. ultimately I want something like this:> > foo("ABD","ADE") > > c(2,1) # 2 in x are in y, 1 in y is not in x > > > foo("AB","ADE") > > c(1,2) # 1 in x is in y, 2 in y are not in x > > Maybe I even do not need the string splitting? > > I hope I was clear in stating my problem. > > Thank you for your valuable input, > David > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
John Fox
2003-Jul-17 22:55 UTC
[R] how to divide a string into characters? - for comparing strings that is
Dear David At 12:07 AM 7/18/2003 +0200, David Andel wrote:>I am searching for a way to do something like "ABC" -> c("A","B","C"). How >can this be accomplished? > >I tried cut() and split(), but they do something else, it seems. > >The purpose for doing this is to find the number of common (and uncommon) >characters, i.e. ultimately I want something like this: > > > foo("ABD","ADE") >c(2,1) # 2 in x are in y, 1 in y is not in x > > foo("AB","ADE") >c(1,2) # 1 in x is in y, 2 in y are not in x > >Maybe I even do not need the string splitting? > >I hope I was clear in stating my problem.Here's a solution based on splitting the strings: > foo <- function(a, b){ + a <- strsplit(a, "")[[1]] + b <- strsplit(b, "")[[1]] + nmatch <- sum(outer(a, b, "==")) + c(nmatch, length(b) - nmatch) + } > foo("ABD","ADE") [1] 2 1 > foo("AB","ADE") [1] 1 2 By the way, apropos() turns up strsplit(): > apropos("split") [1] "split" "split.data.frame" "split.data.frame<-" [4] "split.default" "split.screen" "split<-" [7] "split<-.data.frame" "split<-.default" "strsplit" [10] "unsplit" I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox