Skip to content
Prev 205885 / 398506 Next

string functions

Maybe I don't understand the question. I can think of four ways to
count, none of which give me 7:

a <- "Hello World"
b <- "Hello Peter"

#counting duplicates and the space:
sa <- strsplit(a, split="")[[1]]
sb <- strsplit(b, split="")[[1]]
length(which(sb %in% sa == TRUE))

#counting the space but not the duplicates:
sa <- unique(strsplit(a, split="")[[1]])
sb <- unique(strsplit(b, split="")[[1]])
length(which(sb %in% sa == TRUE))

#counting the duplicates but not the space:
sa <- strsplit(a, split="")[[1]]
sa <- sa[-which(sa == " ")]
sb <- strsplit(b, split="")[[1]]
sb <- sb[-which(sb ==" ")]
length(which(sb %in% sa == TRUE))

#not counting duplicates or the space:
sa <- unique(sa)
sb <- unique(sb)
length(which(sb %in% sa == TRUE))

What am I missing?
On Sat, Jan 9, 2010 at 4:51 PM, Laetitia Schmid <laetitia.schmid at gmx.ch> wrote: