[R] how to divide a string into characters? - for comparing strings that is

Jerome Asselin jerome at hivnet.ubc.ca
Fri Jul 18 00:29:01 CEST 2003


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 (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




More information about the R-help mailing list