0

I am trying to remove a portion of a string. The best I can come up with is to strsplit and then concatenate (maybe there is an easier way.

list<-as.character(c("joe_joe_ID1000", "bob_bob_ID20000"))
list<-strsplit(list, "_")

I would like my output to be "joe joe" and "bob bob" but I am unclear on how to concatenate the resulting strsplit list. And perhaps there is an even easier way Thanks.

3 Answers3

0

One option is to use sub, capture words as a group and then use backreferences (\\1 \\2) of the captured group to format in the specified way

sub("^(\\w+)_(\\w+)_.*", "\\1 \\2", list)
#[1] "joe joe" "bob bob"

NOTE: list is a type. It is better to name objects differently

data

list <- c("joe_joe_ID1000", "bob_bob_ID20000")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can try to use stringr's str_split then paste together using apply.

apply(stringr::str_split(list, "_", simplify = T)[,1:2], 1, paste, collapse=" ")
[1] "joe joe" "bob bob"

Or use a tidyverse

library(tidyverse)
as.tibble(list) %>% 
  separate(value,letters[1:2], sep="_", remove = F, extra = "drop") %>% 
  unite(result, a,b, sep=" ")
# A tibble: 2 x 2
  value           result 
  <chr>           <chr>  
1 joe_joe_ID1000  joe joe
2 bob_bob_ID20000 bob bob

.Last.value %>% pull(result)
[1] "joe joe" "bob bob"
Roman
  • 17,008
  • 3
  • 36
  • 49
0

Using sapply() and paste() you can do this:

sapply(list, function(x) paste(x[1:2], collapse = " "))
[1] "joe joe" "bob bob"

Or something more akin to akrun's solution but slightly different:

c("joe_joe_ID1000", "bob_bob_ID20000") %>% 
  sub("[^_]*$", " ", .) %>%
  gsub("_", " ", ., fixed = TRUE) %>%
  trimws()
[1] "joe joe" "bob bob"

Original data:

list<-as.character(c("joe_joe_ID1000", "bob_bob_ID20000"))
list<-strsplit(list, "_")
s_baldur
  • 29,441
  • 4
  • 36
  • 69