删除第二个数字子文件夹后的所有内容

时间:2018-04-20 12:22:02

标签: r data.table data-manipulation

data <- data.table(column1 = paste0("/url/", c("xyz", "dsas", "ffdsfsd", "fsfddsfds", "ffdfd"))) %>%
                     mutate(column1 = paste0(column1, "/app/"),
                            column1 = paste0(column1, sample(5)),
                            column1 = paste0(column1, "app"))

我想删除第二个子目录之后的内容。我尝试了separate方法,但无法使其正常工作。

1 个答案:

答案 0 :(得分:2)

可能是

setDT(data)[, column1 := sub("\\/[^/]*$", "", column1)]
data$column1
#[1] "/url/xyz/app"       "/url/dsas/app"      "/url/ffdsfsd/app"   "/url/fsfddsfds/app" "/url/ffdfd/app"    

或者

setDT(data)[, column1 := sub("\\/[^/]*\\/[^/]*$", "", column1)]
data$column1
#[1] "/url/xyz"       "/url/dsas"      "/url/ffdsfsd"   "/url/fsfddsfds" "/url/ffdfd"