Re: [R] interval between specific characters in a string...

2022-12-04 Thread Hervé Pagès
On 04/12/2022 00:25, Hadley Wickham wrote: On Sun, Dec 4, 2022 at 12:50 PM Hervé Pagès wrote: On 03/12/2022 07:21, Bert Gunter wrote: Perhaps it is worth pointing out that looping constructs like lapply() can be avoided and the procedure vectorized by mimicking Martin Morgan's solution: ##

Re: [R] interval between specific characters in a string...

2022-12-04 Thread Micha Silver
On 04/12/2022 10:25, Hadley Wickham wrote: On Sun, Dec 4, 2022 at 1:22 PM wrote: This may be a fairly dumb and often asked question about some functions like strsplit() that return a list of things, often a list of ONE thing that be another list or a vector and needs to be made into

Re: [R] interval between specific characters in a string...

2022-12-04 Thread Hadley Wickham
On Sun, Dec 4, 2022 at 1:22 PM wrote: > > This may be a fairly dumb and often asked question about some functions like > strsplit() that return a list of things, often a list of ONE thing that be > another list or a vector and needs to be made into something simpler.. > > The examples shown

Re: [R] interval between specific characters in a string...

2022-12-04 Thread Hadley Wickham
On Sun, Dec 4, 2022 at 12:50 PM Hervé Pagès wrote: > > On 03/12/2022 07:21, Bert Gunter wrote: > > Perhaps it is worth pointing out that looping constructs like lapply() can > > be avoided and the procedure vectorized by mimicking Martin Morgan's > > solution: > > > > ## s is the string to be

Re: [R] interval between specific characters in a string...

2022-12-03 Thread Bert Gunter
Thanks. Very informative. I certainly missed this. -- Bert On Sat, Dec 3, 2022 at 3:49 PM Hervé Pagès wrote: > On 03/12/2022 07:21, Bert Gunter wrote: > > Perhaps it is worth pointing out that looping constructs like lapply() > can > > be avoided and the procedure vectorized by mimicking

Re: [R] interval between specific characters in a string...

2022-12-03 Thread avi.e.gross
022 6:50 PM To: Bert Gunter ; Rui Barradas Cc: r-help@r-project.org; Evan Cooch Subject: Re: [R] interval between specific characters in a string... On 03/12/2022 07:21, Bert Gunter wrote: > Perhaps it is worth pointing out that looping constructs like lapply() > can be avoided an

Re: [R] interval between specific characters in a string...

2022-12-03 Thread Hervé Pagès
On 03/12/2022 07:21, Bert Gunter wrote: Perhaps it is worth pointing out that looping constructs like lapply() can be avoided and the procedure vectorized by mimicking Martin Morgan's solution: ## s is the string to be searched. diff(c(0,grep('b',strsplit(s,'')[[1]]))) However, Martin's

Re: [R] interval between specific characters in a string...

2022-12-03 Thread Bert Gunter
Perhaps it is worth pointing out that looping constructs like lapply() can be avoided and the procedure vectorized by mimicking Martin Morgan's solution: ## s is the string to be searched. diff(c(0,grep('b',strsplit(s,'')[[1]]))) However, Martin's solution is simpler and likely even faster as

Re: [R] interval between specific characters in a string...

2022-12-03 Thread Rui Barradas
Às 17:18 de 02/12/2022, Evan Cooch escreveu: Was wondering if there is an 'efficient/elegant' way to do the following (without tidyverse). Take a string abaaabbabaaab Its easy enough to count the number of times the character 'b' shows up in the string, but...what I'm looking for is

Re: [R] interval between specific characters in a string...

2022-12-02 Thread avi.e.gross
rsplit(st,"")) == "b") indices_shifted <- c(0, head(indices, -1)) result <- indices - indices_shifted There are many other ways to do this and of course some are more straightforward and some more complex. Consider a loop using a vector version of the string where each t

Re: [R] interval between specific characters in a string...

2022-12-02 Thread Andrew Hart via R-help
Here's a function that can get the interval sizes for you. getStringSegmentLengths <- function(s, delim, ...) { nchar(unlist(strsplit(s, delim, ...))) + 1L } It uses strsplit to return a list of all the segments of the string separated by delim. delim can be a regular expression and with

Re: [R] interval between specific characters in a string...

2022-12-02 Thread Martin Morgan
rom: R-help on behalf of Evan Cooch Date: Friday, December 2, 2022 at 6:56 PM To: r-help@r-project.org Subject: [R] interval between specific characters in a string... Was wondering if there is an 'efficient/elegant' way to do the following (without tidyverse). Take a string abaaabb

Re: [R] interval between specific characters in a string...

2022-12-02 Thread Andrew Simmons
try gregexpr('b+', target_string) which looks for one or more b characters, then get the attribute "match.length" On Fri, Dec 2, 2022, 18:56 Evan Cooch wrote: > Was wondering if there is an 'efficient/elegant' way to do the following > (without tidyverse). Take a string > > abaaabbabaaab

[R] interval between specific characters in a string...

2022-12-02 Thread Evan Cooch
Was wondering if there is an 'efficient/elegant' way to do the following (without tidyverse). Take a string abaaabbabaaab Its easy enough to count the number of times the character 'b' shows up in the string, but...what I'm looking for is outputing the 'intervals' between occurrences of