Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string in vector before every string containing pattern

Tags:

r

I have the following vector:

test <- c("here is some text", "here is some other text", "here is my formula", "2+2", "here is my second formula", "4+4", "here is even more text", "here is my final formula", "6+6")

What I'm hoping to do is take every instance of a string containing "formula" and inserting a random string like "CCC" in front of it so that I'd have something like the following:

test <- c("here is some text", "here is some other text", "CCC", "here is my formula", "2+2", "CCC", "here is my second formula", "4+4", "here is even more text", "CCC", "here is my final formula", "6+6")
like image 956
Jacob Avatar asked Apr 19 '26 14:04

Jacob


2 Answers

These use only base R:

1) Use append as shown:

test2 <- test
ix <- rev(grep("formula", test)) - 1
for(i in ix) test2 <- append(test2, "CCC", i)
test2
##  [1] "here is some text"         "here is some other text"  
##  [3] "CCC"                       "here is my formula"       
##  [5] "2+2"                       "CCC"                      
##  [7] "here is my second formula" "4+4"                      
##  [9] "here is even more text"    "CCC"                      
## [11] "here is my final formula"  "6+6"         

2) Here are three different one-liners.

The first creates a matrix whose first row contains "CCC" and NA elements and whose second row is test. It then unravels that and removes the NA's.

The second iterates over test outputting the element if formula is not contained in it or a vector "CCC" followed by the element. This produces a list which is unlisted.

The third prefaces any element containing formula with "CCC\n" and then splits it up.

# 2a
c(na.omit(c(rbind(ifelse(grepl("formula", test), "CCC", NA), test))))

# 2b
unlist(lapply(test, function(x) if (grepl("formula", x)) c("CCC", x) else x))

# 2c
scan(text = sub("(.*formula)", "CCC\n\\1", test), what="", quiet=TRUE, sep="\n")
like image 114
G. Grothendieck Avatar answered Apr 21 '26 05:04

G. Grothendieck


Here is one option in tidyverse

library(dplyr)
library(stringr)
library(tidyr)
tibble(test) %>% 
 uncount(str_detect(test, 'formula') + 1) %>%
  mutate(test = replace(test, duplicated(test, fromLast = TRUE), "CCC"))

-output

# A tibble: 12 × 1
   test                     
   <chr>                    
 1 here is some text        
 2 here is some other text  
 3 CCC                      
 4 here is my formula       
 5 2+2                      
 6 CCC                      
 7 here is my second formula
 8 4+4                      
 9 here is even more text   
10 CCC                      
11 here is my final formula 
12 6+6            
like image 22
akrun Avatar answered Apr 21 '26 04:04

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!