Remove all text before colon

后端 未结 9 1221
天命终不由人
天命终不由人 2020-11-27 12:45

I have a file containing a certain number of lines. Each line looks like this:

TF_list_to_test10004/Nus_k0.345_t0.1_         


        
9条回答
  •  盖世英雄少女心
    2020-11-27 13:40

    I was working on a similar issue. John's and Josh O'Brien's advice did the trick. I started with this tibble:

    library(dplyr)
    my_tibble <- tibble(Col1=c("ABC:Content","BCDE:MoreContent","FG:Conent:with:colons"))
    

    It looks like:

      | Col1 
    1 | ABC:Content 
    2 | BCDE:MoreContent 
    3 | FG:Content:with:colons
    

    I needed to create this tibble:

      | Col1                  | Col2 | Col3 
    1 | ABC:Content           | ABC  | Content 
    2 | BCDE:MoreContent      | BCDE | MoreContent 
    3 | FG:Content:with:colons| FG   | Content:with:colons
    

    And did so with this code (R version 3.4.2).

    my_tibble2 <- mutate(my_tibble
            ,Col2 = unlist(lapply(strsplit(Col1, ':',fixed = TRUE), '[', 1))
            ,Col3 = gsub("^[^:]*:", "", Col1))
    

提交回复
热议问题