> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
> project.org] On Behalf Of Immanuel
> Sent: Thursday, November 04, 2010 3:42 PM
> To: r-help@r-project.org
> Subject: [R] count occurrence and distance of characters in string
> 
> Hello all,
> 
> I want to know how often one character occurs in a given string
> and the distance from between every two occurences. (distance = other
> characters between them).
> 
> thanks
> 

Without a reproducible example, I can only guess.  But this should get you 
started.

s <- 'abcdeabcxdeabcdeaxabcdeabcdeabcdxeabc'
chr.pos <- which(unlist(strsplit(s,NULL)) == 'x')
chr.count <- length(chr.pos)
chr.dist <- diff(chr.pos)-1
chr.pos 
chr.count 
chr.dist

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


______________________________________________
R-help@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.

Reply via email to