r - 函数对给定字符串长度的可变数量的子字符串进行采样

标签 r substring apply replicate

我正在尝试编写一个 R 函数,该函数将根据数据帧每行中原始字符串的长度对可变数量的 5 元素子字符串进行采样。我首先计算了我希望每次绘制重复的次数,并希望将其添加到函数中,以便为每行获取的样本数基于该行的“num_draws”列。我的想法是使用一个通用实例,然后在函数外部使用 apply 语句来作用于每一行,但我不知道如何设置函数来调用 col 3 作为通用实例(不调用仅第一行的值或所有行的值)。

示例数据框:

  BP                             TF                                  num_draws
1 CGGCGCATGTTCGGTAATGA           TFTTTFTTTFFTTFTTTTTF                6
2 ATAAGATGCCCAGAGCCTTTTCATGTACTA TFTFTFTFFFFFFTTFTTTTFTTTTFFTTT      9
3 TCTTAGGAAGGATTC                FTTTTTTTTTFFFFF                     4

期望的输出:

[1]GGCGC FTTTF 
   AATGA TTTTF 
   TTFFT TGTTC 
   TAATG TTTTT
   AATGA TTTTF   
   CGGCG TFTTT

[2]AGATG FTFTF
   ATAAG TFTFT
   ATGCC FTFFF
   GCCCA FFFFF
   ATAAG TFTFT
   GTACT TFFTT
   GCCCA FFFFF
   TGCCC TFFFF
   AGATG FTFTF

[3]TTAGG TTTTT
   CTTAG TTTTT
   GGAAG TTTTT
   GGATT TTFFF

示例代码:

#make example data frame
BaseP1 <- paste(sample(size = 20, x = c("A","C","T","G"), replace = TRUE), collapse = "")
BaseP2 <- paste(sample(size = 30, x = c("A","C","T","G"), replace = TRUE), collapse = "")
BaseP3 <- paste(sample(size = 15, x = c("A","C","T","G"), replace = TRUE), collapse = "")
TrueFalse1 <- paste(sample(size = 20, x = c("T","F"), replace = TRUE), collapse = "")
TrueFalse2 <- paste(sample(size = 30, x = c("T","F"), replace = TRUE), collapse = "")
TrueFalse3 <- paste(sample(size = 15, x = c("T","F"), replace = TRUE), collapse = "")
my_df <- data.frame(c(BaseP1,BaseP2,BaseP3), c(TrueFalse1, TrueFalse2, TrueFalse3)) 


#calculate number of draws by length 
frag_length<- 5 
my_df<- cbind(my_df, (round((nchar(my_df[,1]) / frag_length) * 1.5, digits = 0)))
colnames(my_df) <- c("BP", "TF", "num_draws")

#function to sample x number of draws per row (this does not work)
Fragment = function(string) {
  nStart = sample(1:(nchar(string) -5), 1)
  samp<- substr(string, nStart, nStart + 4)
replicate(n= string[,3], expr = samp)
  }


apply(my_df[,1:2], c(1,2), Fragment)

最佳答案

一种选择是将函数更改为具有另一个参数n,并在replicate调用中创建nStart

Fragment = function(string, n) {
   replicate(n= n,  {nStart <- sample(1:(nchar(string) -5), 1)
                  samp <- substr(string, nStart, nStart + 4)
              })   

}

apply(my_df, 1, function(x) data.frame(lapply(x[1:2], Fragment, n = x[3])))
$`1`
#     BP    TF
#1 GGCGC FFTTF
#2 GGTAA TFFTT
#3 GCGCA TTFTT
#4 CGCAT TFFTT
#5 GGCGC FTTTF
#6 TGTTC FTTFT

#$`2`
#     BP    TF
#1 GTACT TTTTF
#2 ATAAG FTTFT
#3 GTACT TFTFF
#4 TAAGA TTTTF
#5 CCTTT FFTTF
#6 TCATG TTTTF
#7 CCAGA TFTFT
#8 TTCAT TFTFT
#9 CCCAG FTFTF

#$`3`
#     BP    TF
#1 AAGGA TTTFF
#2 AGGAT TTTTT
#3 CTTAG TFFFF
#4 TAGGA TTTFF

关于r - 函数对给定字符串长度的可变数量的子字符串进行采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002224/

相关文章:

r - 将scale_color与类日期变量一起使用会产生错误: as. Date.numeric(value):必须提供 'origin'

r - 在 knitr 文档中使用反引号

substring - Mips中的子串

python - pandas dataframe 使用附加参数应用

r - 如何将数据框的一列划分为另一列?

r - 在 Shiny 应用程序中存储用户上传的数据

python - AttributeError : 'PandasExprVisitor' object has no attribute 'visit_Ellipsis' , 使用 pandas eval

javascript - 尝试在 JavaScript 中实现 Python 风格的 len()

php - 向仅包含数字的日期字符串添加斜杠

string - 无法将类型 'Range<Int>' 的值转换为预期的参数类型 'Range<Index>'(又名 'Range<String.CharacterView.Index>')