Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select every nth character from a string

I have a string of random letters with random spaces and some periods as well. I want to take every nth value (e.g. every 10th) from it. My thought was that if I can transpose it then I can use the row numbers to select for every nth value. Any help is appreciated!

string <- "hutmnycdsldzlkt.ytairuaypk  dq.gubgp hyfjuwvpcdmvqxfcuhapnx"
like image 249
Jeremy Hansen Avatar asked Feb 23 '21 20:02

Jeremy Hansen


3 Answers

To follow-up on OP's idea ("use the row numbers"). Split the string, fill a matrix with 10 rows, select the first row.

matrix(strsplit(x, "")[[1]], nrow = 10)[1, ]
# [1] "h" "d" "r" "." "j" "x"

You will get a recycling warning, but that will not affect us because we select the first row.


Good'ol charToRaw:

rawToChar(charToRaw(x)[c(TRUE, rep(FALSE, 9))])
# [1] "hdr.jx"
like image 95
Henrik Avatar answered Oct 13 '22 13:10

Henrik


We can split the string and use seq to get the elements

v1 <- strsplit(string, "")[[1]]
v1[seq(1, by = 10, length(v1))]
#[1] "h" "d" "r" "." "j" "x"

Or with a regex lookaround

library(stringr)
str_replace_all(string, "(.).{1,9}", "\\1")
#[1] "hdr.jx"

Or make it dynamic with glue

n <- 9
str_replace_all(string, glue::glue("(.).{1,[n]}",
          .open = '[', .close = ']'), "\\1")
#[1] "hdr.jx"
like image 28
akrun Avatar answered Oct 13 '22 13:10

akrun


substring will take a vector of first= and last=, so we can form an appropriate sequence and go from there.

func <- function(x, n, start = 1) {
  vapply(x, function(z) {
    i <- seq.int(start, nchar(z), by = n)
    i <- i[i > 0]
    paste(substring(x, i, i), collapse = "")
  }, character(1))
}

func(string, 10)
# hutmnycdsldzlkt.ytairuaypk  dq.gubgp hyfjuwvpcdmvqxfcuhapnx 
#                                                    "hdr.jx" 

where every 10 (starting at 1) is

hutmnycdsldzlkt.ytairuaypk  dq.gubgp hyfjuwvpcdmvqxfcuhapnx 
12345678901234567890123456789012345678901234567890123456789
^         ^         ^         ^         ^         ^
h         d         r         .         j         x

(The biggest reason I went with an apply variant is in case you have a vector of strings, where substring will work as elegantly.)

like image 5
r2evans Avatar answered Oct 13 '22 13:10

r2evans