r语言 - 在空格后删除



我有这个数据集,我需要删除(删除和垃圾)名称城市之后的一切。我该怎么办?

dati1<- c("a - Novara Delete", "b - Torino Trash", "c - Milano", "f - Bari")

dati2 <-data.frame(do.call(rbind, strsplit(dati1, split = " - ")))

I have try:

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

您可以使用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

或以R为底

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

base R中选择read.table

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

相关内容

  • 没有找到相关文章

最新更新