我有以下函式,它基本上?用替換字串替換bb_seq。
library(tidyverse)
replace_bb_with_str <- function (seed_pattern = NULL, bb_seq = NULL) {
sp <- seed_pattern
gr <- gregexpr("\\? ", sp)
csml <- lapply(gr, function(sp) cumsum(attr(sp, "match.length")))
regmatches(sp, gr) <- lapply(csml, function(sp) substring(bb_seq, c(1, sp[1]), sp))
sp
}
它適用于單次運行:
plist <- c(
"??????????DRHRTRHLAK??????????",
"????????????????????TRCYHIDPHH",
"FKDHKHIDVK????????????????????TRCYHIDPHH",
"FKDHKHIDVK????????????????????"
)
replace_bb_with_str(seed_pattern = plist[1], bb_seq = "ndqeegillkkkkfpssyvv")
# [1] "ndqeegillkDRHRTRHLAKkkkkfpssyvv"
但是當我使用dplyr::mutate運行它時:
expand.grid(seed_pattern = plist, bb_seq = "ndqeegillkkkkfpssyvv") %>%
rowwise() %>%
mutate(nseq = replace_bb_with_str(seed_pattern = seed_pattern, bb_seq = bb_seq))
我收到了這個錯誤:
Error in `mutate()`:
! Problem while computing `nseq = replace_bb_with_str(seed_pattern =
seed_pattern, bb_seq = bb_seq)`.
? The error occurred in row 1.
Caused by error in `nchar()`:
! 'nchar()' requires a character vector
我該如何解決這個問題?
uj5u.com熱心網友回復:
expand.grid()將字符向量強制轉換為與您的函式不匹配的因子。tidyr::expand_grid()保留輸入型別,因此您的函式可以正常作業:
library(tidyr)
expand_grid(seed_pattern = plist, bb_seq = "ndqeegillkkkkfpssyvv") %>%
rowwise() %>%
mutate(nseq = replace_bb_with_str(seed_pattern = seed_pattern, bb_seq = bb_seq))
# A tibble: 4 × 3
# Rowwise:
seed_pattern bb_seq nseq
<chr> <chr> <chr>
1 ??????????DRHRTRHLAK?????????? ndqeegillkkkkfpssyvv ndqeegillkDRHRT…
2 ????????????????????TRCYHIDPHH ndqeegillkkkkfpssyvv ndqeegillkkkkfp…
3 FKDHKHIDVK????????????????????TRCYHIDPHH ndqeegillkkkkfpssyvv FKDHKHIDVKndqee…
4 FKDHKHIDVK???????????????????? ndqeegillkkkkfpssyvv FKDHKHIDVKndqee
請注意,至少對于您的示例資料,實際上不需要使用expand_grid()(而不是data.frame()or tibble())。或者rowwise()——沒有它你會得到相同的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/531693.html
