如何提取子字符串作为dplyr :: mutate管道的一部分

时间:2017-12-04 09:28:52

标签: r regex dplyr tidyverse

我有以下数据框:


library(tidyverse)

df <-  structure(list(pfc_chr = c("chr1", "chr1", "chr1", "chr1", "chr1", 
"chr1", "chr1", "chr1", "chr1", "chr1"), pfc_chr_st = c(3046442L, 
3119671L, 3164756L, 3167322L, 3210838L, 3212196L, 3249068L, 3268246L, 
3444892L, 3451544L), peak_name = c("XXX-ad_peak_1", "XXX-ad_peak_2a", 
"PMN_peak_2", "Ytb_peak_3", "PMN_peak_3", "XXX-ad_peak_6", 
"XXX-ad_peak_8", "PMN_peak_5", "XXX-ad_peak_11", "XXX-ad_peak_12"
)), .Names = c("pfc_chr", "pfc_chr_st", "peak_name"), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))


df
#> # A tibble: 10 x 3
#>    pfc_chr pfc_chr_st      peak_name
#>      <chr>      <int>          <chr>
#>  1    chr1    3046442  XXX-ad_peak_1
#>  2    chr1    3119671 XXX-ad_peak_2a
#>  3    chr1    3164756     PMN_peak_2
#>  4    chr1    3167322     Ytb_peak_3
#>  5    chr1    3210838     PMN_peak_3
#>  6    chr1    3212196  XXX-ad_peak_6
#>  7    chr1    3249068  XXX-ad_peak_8
#>  8    chr1    3268246     PMN_peak_5
#>  9    chr1    3444892 XXX-ad_peak_11
#> 10    chr1    3451544 XXX-ad_peak_12

我想要做的是在peak_name中提取子字符串作为其中的一部分 dplyr管道。最终的期望结果是:

   pfc_chr pfc_chr_st      peak_name        new_col
1     chr1    3046442  XXX-ad_peak_1         XXX-ad
2     chr1    3119671 XXX-ad_peak_2a         XXX-ad
3     chr1    3164756     PMN_peak_2            PMN
4     chr1    3167322     Ytb_peak_3            Ytb
5     chr1    3210838     PMN_peak_3            PMN
6     chr1    3212196  XXX-ad_peak_6         XXX-ad
7     chr1    3249068  XXX-ad_peak_8         XXX-ad
8     chr1    3268246     PMN_peak_5            PMN
9     chr1    3444892 XXX-ad_peak_11         XXX-ad
10    chr1    3451544 XXX-ad_peak_12         XXX-ad

我尝试了但却失败了:

> df %>% mutate(new_col = stringr::str_match(peak_name, "^(.*?)\\_peak\\_*?"))
Error in mutate_impl(.data, dots) : 
  Column `new_col` must be length 10 (the number of rows) or one, not 20

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

我建议stringr::str_extract()使用前瞻:

df %>%
  mutate(new_col = stringr::str_extract(peak_name, "^.*(?=_peak)"))

结果显示如下:

> df %>%
+   mutate(new_col = stringr::str_extract(peak_name, "^.*(?=_peak)"))
# A tibble: 10 x 4
   pfc_chr pfc_chr_st      peak_name new_col
     <chr>      <int>          <chr>   <chr>
 1    chr1    3046442  XXX-ad_peak_1  XXX-ad
 2    chr1    3119671 XXX-ad_peak_2a  XXX-ad
 3    chr1    3164756     PMN_peak_2     PMN
 4    chr1    3167322     Ytb_peak_3     Ytb
 5    chr1    3210838     PMN_peak_3     PMN
 6    chr1    3212196  XXX-ad_peak_6  XXX-ad
 7    chr1    3249068  XXX-ad_peak_8  XXX-ad
 8    chr1    3268246     PMN_peak_5     PMN
 9    chr1    3444892 XXX-ad_peak_11  XXX-ad
10    chr1    3451544 XXX-ad_peak_12  XXX-ad

请注意,“_peak_8”等数据会返回一个空字符串;诸如“peak_8”之类的数据返回NA

答案 1 :(得分:1)

选择第二列。

df %>% mutate(new_col = stringr::str_match(peak_name, "^(.*?)\\_peak\\_*?")[, 2])

输出

    pfc_chr pfc_chr_st      peak_name new_col
1    chr1    3046442  XXX-ad_peak_1  XXX-ad
2    chr1    3119671 XXX-ad_peak_2a  XXX-ad
3    chr1    3164756     PMN_peak_2     PMN
4    chr1    3167322     Ytb_peak_3     Ytb
5    chr1    3210838     PMN_peak_3     PMN
6    chr1    3212196  XXX-ad_peak_6  XXX-ad
7    chr1    3249068  XXX-ad_peak_8  XXX-ad
8    chr1    3268246     PMN_peak_5     PMN
9    chr1    3444892 XXX-ad_peak_11  XXX-ad
10    chr1    3451544 XXX-ad_peak_12  XXX-ad