我使用dplyr完成了以下过程而没有任何问题:
library(tidyverse)
my_dplyr_dat <- structure(list(chrn = c("chr20", "chr6", "chr5"), start = c(52447674L,
12962440L, 66453982L), end = c(52447689L, 12962455L, 66453997L
), motif_name_binned = c("ZNF263/MA0528.1/Jaspar.instid_chr20:52447338-52447738.bin22",
"Klf12/MA0742.1/Jaspar.instid_chr6:12962360-12962760.bin6", "Hoxc9/MA0485.1/Jaspar.instid_chr5:66453806-66454206.bin12"
), motif_score = c(6.728401, -0.979777, 6.091471), strand = c("+",
"+", "+"), read_count = c(0L, 0L, 0L)), .Names = c("chrn", "start",
"end", "motif_name_binned", "motif_score", "strand", "read_count"
), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
看起来像这样:
# A tibble: 3 x 7
chrn start end motif_name_binned motif_score strand read_count
<chr> <int> <int> <chr> <dbl> <chr> <int>
1 chr20 52447674 52447689 ZNF263/MA0528.1/Jaspar.instid_chr20:52447338-52447738.bin22 6.728401 + 0
2 chr6 12962440 12962455 Klf12/MA0742.1/Jaspar.instid_chr6:12962360-12962760.bin6 -0.979777 + 0
3 chr5 66453982 66453997 Hoxc9/MA0485.1/Jaspar.instid_chr5:66453806-66454206.bin12 6.091471 + 0
我希望在那里实现的主要任务是使用正则表达式提取motif_name_binned
列并将其扩展为3列c('motif', 'inst', 'binno')
,使用dplyr可以这样做:
my_dplyr_dat %>%
extract(motif_name_binned, c('motif', 'inst', 'binno'), regex = "^(.*?\\/.*?)\\.instid_(.*?)\\.bin(\\d+)", remove = FALSE) %>%
select(-motif_name_binned)
产生这个:
# A tibble: 3 x 9
chrn start end motif inst binno motif_score strand read_count
* <chr> <int> <int> <chr> <chr> <chr> <dbl> <chr> <int>
1 chr20 52447674 52447689 ZNF263/MA0528.1/Jaspar chr20:52447338-52447738 22 6.728401 + 0
2 chr6 12962440 12962455 Klf12/MA0742.1/Jaspar chr6:12962360-12962760 6 -0.979777 + 0
3 chr5 66453982 66453997 Hoxc9/MA0485.1/Jaspar chr5:66453806-66454206 12 6.091471 + 0
如何使用data.table进行操作?
这是我拥有的data.table格式的原始数据(即在字符串提取之前等):
library(data.table)
my_data_table <- structure(list(chrn = c("chr20", "chr6", "chr5"), start = c(52447674L,
12962440L, 66453982L), end = c(52447689L, 12962455L, 66453997L
), motif_name_binned = c("ZNF263/MA0528.1/Jaspar.instid_chr20:52447338-52447738.bin22",
"Klf12/MA0742.1/Jaspar.instid_chr6:12962360-12962760.bin6", "Hoxc9/MA0485.1/Jaspar.instid_chr5:66453806-66454206.bin12"
), motif_score = c(6.728401, -0.979777, 6.091471), strand = c("+",
"+", "+"), read_count = c(0L, 0L, 0L)), .Names = c("chrn", "start",
"end", "motif_name_binned", "motif_score", "strand", "read_count"
), class = c("data.table", "data.frame"), row.names = c(NA, -3L
))
看起来像这样:
chrn start end motif_name_binned motif_score strand read_count
1: chr20 52447674 52447689 ZNF263/MA0528.1/Jaspar.instid_chr20:52447338-52447738.bin22 6.728401 + 0
2: chr6 12962440 12962455 Klf12/MA0742.1/Jaspar.instid_chr6:12962360-12962760.bin6 -0.979777 + 0
3: chr5 66453982 66453997 Hoxc9/MA0485.1/Jaspar.instid_chr5:66453806-66454206.bin12 6.091471 + 0
答案 0 :(得分:4)
我们创建了一个唯一的分割字符gsub
和tstrsplit
根据字符分成3列
my_data_table[, c('motif', 'inst', 'binno') := tstrsplit(
gsub("^(.*?\\/.*?)\\.instid_(.*?)\\.bin(\\d+)", "\\1$\\2$\\3", motif_name_binned), '$',
fixed = TRUE)][, setdiff(names(my_data_table), "motif_name_binned"), with = FALSE]
# chrn start end motif_score strand read_count motif inst binno
#1: chr20 52447674 52447689 6.728401 + 0 ZNF263/MA0528.1/Jaspar chr20:52447338-52447738 22
#2: chr6 12962440 12962455 -0.979777 + 0 Klf12/MA0742.1/Jaspar chr6:12962360-12962760 6
#3: chr5 66453982 66453997 6.091471 + 0 Hoxc9/MA0485.1/Jaspar chr5:66453806-66454206 12