You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R语言查找逗号分隔字符串间的肽段重复次数?

解决方法

假设你已经通过循环收集了所有肽段拼接后的字符串到一个向量(比如peptide_strings),可以按以下步骤统计各肽段的跨组重复次数:

  1. 收集所有拼接字符串
    先修正循环里的代码,不要直接print,而是把每次生成的字符串存到向量里:
# 初始化空向量
peptide_strings <- c()
# 你的循环逻辑(示例)
for (i in 1:your_loop_times) {
  # ... 你的数据过滤处理得到data.final ...
  current_string <- paste(data.final$Peptide, collapse = ',')
  peptide_strings <- c(peptide_strings, current_string)
}
  1. 拆分所有字符串为单个肽段
    把每个逗号分隔的字符串拆分成独立肽段,合并成一个一维向量:
# 拆分并去空值(防止空字符串干扰)
all_peptides <- unlist(strsplit(peptide_strings, ','))
# 去除可能的首尾空格(如果肽段存在格式问题)
all_peptides <- trimws(all_peptides)
# 移除空元素(如果有的话)
all_peptides <- all_peptides[all_peptides != ""]
  1. 统计重复次数
    用基础R的table函数直接统计,或者用tidyverse工具更灵活:
  • 基础R方式:
    # 生成频率表
    peptide_counts <- table(all_peptides)
    # 转成数据框方便查看
    peptide_counts_df <- as.data.frame(peptide_counts, stringsAsFactors = FALSE)
    colnames(peptide_counts_df) <- c("Peptide", "Repeat_Count")
    # 按重复次数降序排序
    peptide_counts_df <- peptide_counts_df[order(-peptide_counts_df$Repeat_Count), ]
    
  • tidyverse方式(需要先加载dplyr):
    library(dplyr)
    peptide_counts_df <- tibble(Peptide = all_peptides) %>%
      count(Peptide, name = "Repeat_Count", sort = TRUE)
    

这样得到的peptide_counts_df里,Repeat_Count就是对应肽段在不同行(不同处理组)的重复次数。

内容的提问来源于stack exchange,提问作者C15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 08:40:42