Skip to content
Prev 279083 / 398506 Next

Counting the occurences of a charater within a string

I used within and vapply:

x <- data.frame(Col1 = c("abc/def", "ghi/jkl/mno"), stringsAsFactors = FALSE)
count.slashes <- function(string)sum(unlist(strsplit(string, NULL)) ==
"/")within(x, Col2 <- vapply(Col1, count.slashes, 1))
? ? ? ? ?Col1 Col21 ? ? abc/def ? ?12 ghi/jkl/mno ? ?2
On Thu, Dec 1, 2011 at 1:05 PM, Bert Gunter <gunter.berton at gene.com> wrote: