'Delete after the space

I have this dataset I need to delete (Delete and Trash) everything after the name city. How can I do?

dati1<- c("a - Novara Delete", "b - Torino Trash", "c - Milano", "f - Bari")
    
dati2 <-data.frame(do.call(rbind, strsplit(dati1, split = " - ")))

I have tried:

c <- dati2$X2 %>%  mutate(dati2$X2 = sub("\\s+[^ ]+$", "", dati2$X2))


Solution 1:[1]

You can use separate:

tidyr::separate(data.frame(dati1), col = dati1, into = stringr::str_c("col", 1:2), extra = 'drop')

  col1   col2
1    a Novara
2    b Torino
3    c Milano
4    f   Bari

or with base R

data.frame(do.call(rbind, lapply(strsplit(dati1, split = "[^[:alnum:]]+"), head, 2)))

Solution 2:[2]

An option with read.table from base R

read.table(text = sub("^(\\S+ - \\S+)\\s+.*", "\\1", dati1),
     header = FALSE, sep = "-", strip.white = TRUE)
   V1     V2
1  a Novara
2  b Torino
3  c Milano
4  f   Bari

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2