[R] character occurence within a string

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon Feb 21 13:23:27 CET 2005


a simple solution is:

vec.strings <- c("ab|c|d", "a|b|c", "a", "|a|b|c|d")
f <- function(pat, vec.strings) sapply(strsplit(vec.strings, pat, 
fixed=TRUE), length)-1
f("|", vec.strings)

but probably there are better proposals.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Marc Mamin" <M.Mamin at intershop.de>
To: <R-help at stat.math.ethz.ch>
Sent: Monday, February 21, 2005 12:35 PM
Subject: [R] character occurence within a string


> Hello,
>
> I'm looking for a function that counts the occurences of a given 
> character within a string.
>
> f('|','ab|c|d') => 2
>
>
> More precisely, I need to complete a vector of strings to ensure 
> that all elements have the same count of a "separator":
>
> a|b|c
> a
> |a|b|c|d
>
> =>
>
> a|b|c||
> a||||
> |a|b|c|d
>
> I guess that scan makes use of an internal function that would do 
> the job...
>
>
> Thanks,
>
> Marc Mamin
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list