Re: [R] Frequency of a character in a string

2016-11-14 Thread Hervé Pagès
On 11/14/2016 12:44 PM, Bert Gunter wrote: (Sheepishly)... Yes, thank you Hervé. It would have been nice if I had given correct soutions. Fixed = TRUE could not have of course worked with ["a"] character class! Here's what I found with a 10 element vector each member of which is a 1e5 length

Re: [R] Frequency of a character in a string

2016-11-14 Thread William Dunlap via R-help
Here is another variant, v3, and a change to your first example so it returns the same value as your second example. > set.seed(1001) > x <- sapply(1:100, function(x)paste0(sample(letters,rpois(1,1e5),rep=TRUE),collapse = "")) > system.time(v1 <- lengths(strsplit(paste0("X", x,

Re: [R] Frequency of a character in a string

2016-11-14 Thread Bert Gunter
(Sheepishly)... Yes, thank you Hervé. It would have been nice if I had given correct soutions. Fixed = TRUE could not have of course worked with ["a"] character class! Here's what I found with a 10 element vector each member of which is a 1e5 length string: >

Re: [R] Frequency of a character in a string

2016-11-14 Thread Hervé Pagès
Hi, FWIW using gsub( , fixed=TRUE) is faster than using gsub( , fixed=FALSE) or strsplit( , fixed=TRUE): set.seed(1) Vec <- paste(sample(letters, 500, replace = TRUE), collapse = "") system.time(res1 <- nchar(gsub("[^a]", "", Vec))) # user system elapsed # 0.585 0.000 0.586

Re: [R] Frequency of a character in a string

2016-11-14 Thread Bert Gunter
Chuck, Marc, and anyone else who still has interest in this odd little discussion ... Yes, and with fixed = TRUE my approach took 1/3 as much time as Chuck's with a 10 element vector each element of which is a character string of length 1e5: > set.seed(1001) > x <- sapply(1:10,

Re: [R] Frequency of a character in a string

2016-11-14 Thread Charles C. Berry
On Mon, 14 Nov 2016, Marc Schwartz wrote: On Nov 14, 2016, at 11:26 AM, Charles C. Berry wrote: On Mon, 14 Nov 2016, Bert Gunter wrote: [stuff deleted] Hi, Both gsub() and strsplit() are using regex based pattern matching internally. That being said, they are

Re: [R] Frequency of a character in a string

2016-11-14 Thread Marc Schwartz
> On Nov 14, 2016, at 11:26 AM, Charles C. Berry wrote: > > On Mon, 14 Nov 2016, Bert Gunter wrote: > >> 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")) >>>

Re: [R] Frequency of a character in a string

2016-11-14 Thread Charles C. Berry
On Mon, 14 Nov 2016, Bert Gunter wrote: 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

Re: [R] Frequency of a character in a string

2016-11-14 Thread Bert Gunter
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

Re: [R] Frequency of a character in a string

2016-11-14 Thread Brijesh Mishra
?nchar in the base R should also help... On Mon, Nov 14, 2016 at 2:26 PM, Ismail SEZEN wrote: > > > On 14 Nov 2016, at 11:44, Ferri Leberl wrote: > > > > > > Dear All, > > Is there a function to count the occurences of a certain character in a >

Re: [R] Frequency of a character in a string

2016-11-14 Thread Ismail SEZEN
> On 14 Nov 2016, at 11:44, Ferri Leberl 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] Frequency of a character in a string

2016-11-14 Thread Ferri Leberl
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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see