以降序计数字母频率

时间:2019-04-19 17:05:45

标签: r character frequency-distribution

我有一个类似这样的数据

df<-structure(list(col = structure(c(9L, 2L, 13L, 11L, 5L, 7L, 10L, 
6L, 8L, 3L, 12L, 4L, 1L), .Label = c("HHRGGVCTS", "MGSSN", "MVKTTYYDVG", 
"RRHYNGAYDD", "RTSTN", "S", "SNCWC", "sp|P31689|DNJA1_HUMAN DnaJ homolog GN=DNAJA1 PE=1 SV=2  ", 
"sp|Q9H9K5|MER34_HUMAN Endogenous PE=1 SV=1", "THYDT", "TVHAV", 
"VCMCVVDDNR", "YATTA"), class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))

我正在尝试计算字母频率。我想在每行中计算20个可能的字母。

例如

  1. 第一行:行以sp|开始,因此不会计算字符频率,结果是原始字符串
  2. 第二行:不是以sp|开头,因此会显示字符频率
MGSSN  2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

这意味着有2个S,1,M,1,G,1,N,其他字母为空。

字符频率按降序排列。

最终输出如下所示

output<-structure(list(col = structure(c(9L, 2L, 13L, 11L, 5L, 7L, 10L, 
6L, 8L, 3L, 12L, 4L, 1L), .Label = c("HHRGGVCTS", "MGSSN", "MVKTTYYDVG", 
"RRHYNGAYDD", "RTSTN", "S", "SNCWC", "sp|P31689|DNJA1_HUMAN DnaJ homolog GN=DNAJA1 PE=1 SV=2  ", 
"sp|Q9H9K5|MER34_HUMAN Endogenous PE=1 SV=1", "THYDT", "TVHAV", 
"VCMCVVDDNR", "YATTA"), class = "factor"), Col2 = structure(c(8L, 
2L, 3L, 2L, 2L, 2L, 2L, 1L, 7L, 5L, 6L, 5L, 4L), .Label = c("1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", "2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,2,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0", "2,2,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0", 
"3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0", "sp|P31689|DNJA1_HUMAN DnaJ homolog GN=DNAJA1 PE=1 SV=2  ", 
"sp|Q9H9K5|MER34_HUMAN Endogenous PE=1 SV=1"), class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))

1 个答案:

答案 0 :(得分:1)

我们可以使用str_count

library(stringr)
i1 <- !grepl("^sp", df$col)
df$col2[i1] <- sapply(as.character(df$col[i1]), function(x)
     paste(sort(str_count(x, LETTERS), decreasing = TRUE), collapse=", "))
df$col2[!i1] <- df$col[!i1]

或者也可以将其保留为list列,而不是保留为字符串

library(tidyverse)
df %>%
    mutate(col = as.character(col),
            col2 = map(col, ~ if(str_detect(.x, "^sp")) .x 
               else str_count(.x, LETTERS) %>% 
             sort(decreasing = TRUE)))