0

Hi:) what i'm trying to do is write a simple program to expand from shortest entry

for example

a-z

to longest write

for example

abcdefghijklmnouprstwxyz

Any ideas on how to implement that in R? Thanks

Phil
  • 7,287
  • 3
  • 36
  • 66
Garn_R
  • 77
  • 5
  • I think this should give you some ideas. Possible duplicate of https://stackoverflow.com/questions/53476833/create-a-sequence-between-two-letters – Ronak Shah Sep 13 '21 at 04:31

1 Answers1

1

We could do

paste(letters[do.call(`:`, as.list(match( strsplit(str1, "-")[[1]], 
       letters)))], collapse="")
[1] "abcdefghijklmnopqrstuvwxyz"

The above could be wrapped in a function

f1 <- function(string1) {
     paste(letters[do.call(`:`, as.list(match( strsplit(string1, "-")[[1]], 
       letters)))], collapse="")
}

Using the new data

i1 <- grep('-', df$letters)
df$letters[i1] <- sapply(df$letters[i1], f1)

df$letters
[1] "abc"  "bcd"  "cd"   "abcd" "ab"   "cd"  

Or use regex

paste(grep(sprintf('[%s]', str1), letters, value = TRUE), collapse="")

Based on the new dataset

library(dplyr)
library(tidyr)
library(stringr)
df %>% 
   separate(letters, into = c('start', 'end'), 
         sep="(?<=[a-z])-?(?=[a-z])") %>% 
   transmute(new = map2_chr(match(start, letters),
       match(end, letters), ~ str_c(letters[.x:.y], collapse="")))

-output

  new
1  abc
2  bcd
3   cd
4 abcd
5   ab
6   cd

data

str1 <- "a-z"
df<-data.frame(letters=c("a-c","b-d","c-d","a-d", "ab", "cd")) 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • How would you approach the same issue in a dataset like below? df<-data.frame(letters=c("a-c","b-d","c-d","a-d", "ab", "cd")) – Garn_R Sep 12 '21 at 19:23
  • @ViniciusGarnica where is the dataset you mentioned as below – akrun Sep 12 '21 at 19:23
  • @ViniciusGarnica in some of the elements there are no `-`? Is it a typo – akrun Sep 12 '21 at 19:25
  • @ViniciusGarnica already updated the answer. I used tidyverse. But, the other one should also work if we loop with lapply/sapply and apply the function f1. I think based on the pattern you only need to loop where the `-` exists – akrun Sep 12 '21 at 19:30