[R] Frequency of a character in a string
Bert Gunter
bgunter.4567 at gmail.com
Mon Nov 14 17:09:58 CET 2016
Yes, but it need some help, since nchar gives the length of the
*entire* string; e.g.
## to count "a" 's :
> x <-(c("abbababba","bbabbabbaaaba"))
> nchar(gsub("[^a]","",x))
[1] 4 6
This is one of about 8 zillion ways to do this in base R if you don't
want to use a specialized package.
Just for curiosity: Can anyone comment on what is the most efficient
way to do this using base R pattern matching?
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Mon, Nov 14, 2016 at 5:52 AM, Brijesh Mishra
<brijeshkmishra at gmail.com> wrote:
> ?nchar in the base R should also help...
>
> On Mon, Nov 14, 2016 at 2:26 PM, Ismail SEZEN <sezenismail at gmail.com> wrote:
>
>>
>> > On 14 Nov 2016, at 11:44, Ferri Leberl <ferri.leberl at gmx.at> wrote:
>> >
>> >
>> > Dear All,
>> > Is there a function to count the occurences of a certain character in a
>> string resp. in a vector of strings?
>> > Thank you in advance!
>> > Yours, Ferri
>> >
>>
>> library(stringr)
>> ?str_count
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
More information about the R-help
mailing list