找到字符串中的大写字母

don*_*ati 1 r plyr stringr

我想在每个字符串中找到大写字母,并计算每个字符串的数量,例如

t = c("gctaggggggatggttactactGtgctatggactac", "gGaagggacggttactaCgTtatggactacT", "gcGaggggattggcttacG")  

ldply(str_match_all(t,"[A-Z]"),length)
Run Code Online (Sandbox Code Playgroud)

当应用上述功能时,我的输出是

1 4 2
Run Code Online (Sandbox Code Playgroud)

但我的愿望输出是

[1] G -1

[2] G -1 C -1 T -2

[3] G -2

tal*_*lat 5

您可以提取所有大写字母,然后使用表格计算频率:

library(stringr)
lapply(str_extract_all(t, "[A-Z]"), table)
# [[1]]
# 
# G 
# 1 
# 
# [[2]]
# 
# C G T 
# 1 1 2 
# 
# [[3]]
# 
# G 
# 2 
Run Code Online (Sandbox Code Playgroud)