Re: [ESS] Possible problem with Org 9.6?
> William Denton via ESS-help writes: > On 2 December 2022, Colin Baxter wrote: >> I have now used your set-up, exactly I think, using "emacs-28.2 >> -q" and the latest ESS and I still cannot reproduce your >> error. For me, the src source block gives the expected >> result. I'm stuck! > Strange! But it's been fixed in Org, so whatever caused it can > remain a mystery. Excellent. I'm pleased R + org are now working to your satisfaction. __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [R] interval between specific characters in a string...
Evan, there are oodles of ways to do many things in R, and mcu of what the tidyverse supplies can often be done as easily, or easier, outside it. Before presenting a solution, I need to make sure I am answering the same question or problem you intend. Here is the string you have as an example: st <- "abaaabbabaaab" Is the string a string testing for single characters called "b" with any other characters being either just "a" or at least non-"b" and of any length but at least a few? If so, ONE METHOD is to convert the string to a vector for reasons that will become clear. For oddball reasons, this is a way to do it: > unlist(strsplit(st,"")) [1] "a" "b" "a" "a" "a" "b" "b" "a" "a" "a" "a" "a" "b" "a" "a" "a" "b" The result is a vector you can examine to see if they are equal to "b" or not as a TRUE/FALSE vector: > unlist(strsplit(st,"")) == "b" [1] FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE [17] TRUE Now you can ask for the indices which are TRUE, meaning at what offset from the beginning are there instances of the letter "b": > which(unlist(strsplit(st,"")) == "b") [1] 2 6 7 13 17 This shows the second the integer offsets for the letter "b" are the second, sixth and so on to seventeenth. Again, if I understood you, you want a measure of how far apart instances of "b" are with adjacent ones being 1 apart. Again, many methods but I chose one where I sort of slid over the above values by sliding in a zero from the front and removing the last entry. So save that in a variable first: indices <- which(unlist(strsplit(st,"")) == "b") indices_shifted <- c(0, head(indices, -1)) The two contain: > indices [1] 2 6 7 13 17 > indices_shifted [1] 0 2 6 7 13 > indices - indices_shifted [1] 2 4 1 6 4 The above is the same as your intended result. If you want to be cautious, handle edge cases like not having any "b" or an empty string. Here is the consolidated code: st <- "abaaabbabaaab" indices <- which(unlist(strsplit(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 time you see a b", you remember the last index you saw it and put out the number representing the gap. Fairly low tech. -Original Message- From: R-help On Behalf Of Evan Cooch Sent: Friday, December 2, 2022 12:19 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 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 'b' (starting the counter at the beginning of the string). So, for the preceding example, 'b' shows up in positions 2, 6, 7, 13, 17 So, the interval data would be: 2, 4, 1, 6, 4 My main approach has been to simply output positions (say, something like unlist(gregexpr('b', target_string))), and 'do the math' between successive positions. Can anyone suggest a more elegant approach? Thanks in advance... __ R-help@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. __ R-help@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.
Re: [R] interval between specific characters in a string...
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 ..., you can pass all the extra options to strsplit in order to specify how to break up the string. It then uses unlist to convert the list output of strsplit to a character vector. nchar then gives the lengths of all the elements of the character vector and finally a 1 is added to each of these in order to obtain the correct interval sizes. Hth, Andrew. On 2/12/2022 14:18, Evan Cooch wrote: 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 'b' (starting the counter at the beginning of the string). So, for the preceding example, 'b' shows up in positions 2, 6, 7, 13, 17 So, the interval data would be: 2, 4, 1, 6, 4 My main approach has been to simply output positions (say, something like unlist(gregexpr('b', target_string))), and 'do the math' between successive positions. Can anyone suggest a more elegant approach? Thanks in advance... __ R-help@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. __ R-help@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.
Re: [R] interval between specific characters in a string...
You could split the string into letters and figure out which ones are �b� which(strsplit(x, "")[[1]] == "b") and then find the difference between each position, �anchoring� at position 0 > diff(c(0, which(strsplit(x, "")[[1]] == "b"))) [1] 2 4 1 6 4 From: 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 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 'b' (starting the counter at the beginning of the string). So, for the preceding example, 'b' shows up in positions 2, 6, 7, 13, 17 So, the interval data would be: 2, 4, 1, 6, 4 My main approach has been to simply output positions (say, something like unlist(gregexpr('b', target_string))), and 'do the math' between successive positions. Can anyone suggest a more elegant approach? Thanks in advance... __ R-help@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@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.
Re: [R] interval between specific characters in a string...
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 > > 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 'b' (starting the counter at the beginning of the > string). So, for the preceding example, 'b' shows up in positions > > 2, 6, 7, 13, 17 > > So, the interval data would be: 2, 4, 1, 6, 4 > > My main approach has been to simply output positions (say, something > like unlist(gregexpr('b', target_string))), and 'do the math' between > successive positions. Can anyone suggest a more elegant approach? > > Thanks in advance... > > __ > R-help@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@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.
[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 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 'b' (starting the counter at the beginning of the string). So, for the preceding example, 'b' shows up in positions 2, 6, 7, 13, 17 So, the interval data would be: 2, 4, 1, 6, 4 My main approach has been to simply output positions (say, something like unlist(gregexpr('b', target_string))), and 'do the math' between successive positions. Can anyone suggest a more elegant approach? Thanks in advance... __ R-help@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.
Re: [ESS] Possible problem with Org 9.6?
On 2 December 2022, Colin Baxter wrote: I have now used your set-up, exactly I think, using "emacs-28.2 -q" and the latest ESS and I still cannot reproduce your error. For me, the src source block gives the expected result. I'm stuck! Strange! But it's been fixed in Org, so whatever caused it can remain a mystery. R and Org are back to working nicely (except for a problem with images not being refreshed, but that's to do with images generally, not just R). commit 4564627415b1e609ac6e92b33d51c8e427c1 (origin/bugfix) Author: Ihor Radchenko Date: Fri Dec 2 13:57:14 2022 +0800 org-babel-comit-with-output: Fix when prompt is emitted partially * lisp/ob-comint.el (org-babel-comint-with-output): Do not rely on output filter when detecting prompt lines. Search for `comint-prompt-regexp' in the full output after applying the filter. This is needed when the filter is supplied with partial prompts. Cheers, Bill -- William Denton https://www.miskatonic.org/ Librarian, artist and licensed private investigator. Toronto, Canada __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [ESS] Possible problem with Org 9.6?
Dear William, > William Denton via ESS-help writes: > On 1 December 2022, Colin Baxter via ESS-help wrote: >> My last post was mangled. The "Nor me" referred to a satisfactory >> output of the R code - not to "haven't a clue"! >> >> Are you using the latest ESS from git? And have you set the >> "ess-startup-directory" to default-directory? > I retried with this and still have the problem. Here's how I did > it, with current Org (d500b406fc) and ESS (01e7f5b482) source > trees. > In the Org source, run "make repro". > In the *scratch* buffer, C-x C-e each of these lines (adjust path > for ESS source as needed): > (add-to-list 'load-path "/usr/local/src/ESS/lisp") (require > 'ess-r-mode) (require 'ob-R) > Load an Org file with this simple block: > #+begin_src R :results output :session *R* x <- 2:4 print(x) > #+end_src > C-c C-c on the begin line, say yes to executing the code, use the > default for the working directory. > Results: #+RESULTS line appears, but no output. > Change "output" to "value" and it works. Remove the :session > argument and it works. > Kill Emacs, execute the Lisp as above, but this time add: > (setq ess-startup-directory 'default-directory) > Perform the other steps, and #+RESULTS is still empty. > I hope this may help narrow it down. There's some discussion on > the Org list about this too: > https://lists.gnu.org/archive/html/emacs-orgmode/2022-12/msg1.html I have now used your set-up, exactly I think, using "emacs-28.2 -q" and the latest ESS and I still cannot reproduce your error. For me, the src source block gives the expected result. I'm stuck! Best wishes, Colin Baxter. __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [ESS] Possible problem with Org 9.6?
Ok, it's not ob-R. If I load ob-R I still get #+begin_src R :results output :session *R* :exports both x <- 2:4 print(x) #+end_src #+Results[5cafbcfc3677a0d07c696e39397c23e61ba0c021]: : [1] 2 3 4 __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [ESS] Possible problem with Org 9.6?
Hello William, > William Denton via ESS-help writes: > On 1 December 2022, Colin Baxter via ESS-help wrote: >> My last post was mangled. The "Nor me" referred to a satisfactory >> output of the R code - not to "haven't a clue"! >> >> Are you using the latest ESS from git? And have you set the >> "ess-startup-directory" to default-directory? > I retried with this and still have the problem. Here's how I did > it, with current Org (d500b406fc) and ESS (01e7f5b482) source > trees. > In the Org source, run "make repro". > In the *scratch* buffer, C-x C-e each of these lines (adjust path > for ESS source as needed): > (add-to-list 'load-path "/usr/local/src/ESS/lisp") (require > 'ess-r-mode) (require 'ob-R) > Load an Org file with this simple block: > #+begin_src R :results output :session *R* x <- 2:4 print(x) > #+end_src > C-c C-c on the begin line, say yes to executing the code, use the > default for the working directory. > Results: #+RESULTS line appears, but no output. > Change "output" to "value" and it works. Remove the :session > argument and it works. > Kill Emacs, execute the Lisp as above, but this time add: > (setq ess-startup-directory 'default-directory) > Perform the other steps, and #+RESULTS is still empty. > I hope this may help narrow it down. There's some discussion on > the Org list about this too: > https://lists.gnu.org/archive/html/emacs-orgmode/2022-12/msg1.html I'll use your exactly setup later in the day and see if I can reproduce the problem. In the meanwhile, however, I notice you use ob-R. I do not. I have never used it. In my ~/.emacs, I just have #+begin_src emacs-lisp (org-babel-do-load-languages 'org-babel-load-languages '( (R . t) ;; Other settings )) #+end_src This has always worked. So perhaps the issue lies with ob-R, and this is what I'm missing. Best wishes, Colin Baxter. __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help