I want to store the first character of a string in a variable and the rest of the string in another variable. For instance:
x <- "foo" prefix <- substr(x, 1, 1) suffix <- substring(x, 2)
However, it seems wasteful to call substrand substring. Is there no way to extract both the substring and the rest of the string (the "difference" between the substring and the source string)?
substr
substring
Maybe something like this:
substring(x, c(1, 2), c(1, nchar(x))) # [1] "f" "oo"
Here is an idea using regex,
strsplit(gsub('^([A-z]{1})([A-z]+)$', '\\1_\\2', x), '_') #[[1]] #[1] "f" "oo"
str_split stringr:
str_split
stringr
require(stringr) x<-c("foo", "hello", "world") str_split(x,"(?<=.{1})",2) #[[1]] #[1] "f" "oo" #[[2]] #[1] "h" "ello" #[[3]] #[1] "w" "orld"
separate tidyr
separate
tidyr
library(tidyr) separate(data_frame(x), x, into = c('prefix', 'suffix'), sep=1) # A tibble: 3 × 2 # prefix suffix #* <chr> <chr> #1 f oo #2 h ello #3 w orld
x<-c("foo", "hello", "world")
Source: https://habr.com/ru/post/1675349/More articles:how to attract communities with networkx - pythonSort dict of dict in jinja2 loop - pythonArrow function and short circuit rating - javascriptPaypal Как переопределить определение платежа? - paypalReconfigure dependencies when testing integration of ASP.NET Core Web API and EF Core - c #ASP.NET Core UseSetting from Integration Test - c #Access FontAwesome unicode as a class - javascriptAmazon S3 GUI client for OS X that enables AWS STS to take on the role - amazon-s3Skip docker argument - dockerSet maxHeight to alert javafx with scrollpane - javaAll Articles