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
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, "X"),"a",fixed=TRUE
(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:
> system.time((lengths(strsplit(paste0
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
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, function(x)paste0(
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 ultimately calling .Intern
> 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"))
>>> nchar(gsub("[^a]","",x)
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
wan
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
?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
> string resp. in a vector of strings?
> > Thank yo
> 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
_
11 matches
Mail list logo