计算字符串的外观和上面行中的归属结果

时间:2017-05-30 12:22:12

标签: r pattern-matching sax string-matching sliding-window

我有一个这样的数据框:

df <- data.frame(value = c("a","b","b","d","a","b","b","d","a","b","c","d"), 
             pattern = c("NA","a","ab","abb","bbd","bda","dab","abb","bbd","bda","dab","abc"))

值列指示实际行为,模式显示此操作发生之前的累积行为。 现在我想将模式与上面的4种模式进行比较,并计算出现次数,加上“值”列中所属字母的出现次数,以计算预期结果。

结果应如下所示:

   value pattern apperance a b c d exp.result
1      a      NA      0    0 0 0 0       <NA>
2      b       a      0    0 0 0 0       <NA>
3      b      ab      0    0 0 0 0       <NA>
4      d     abb      0    0 0 0 0       <NA>
5      a     bbd      0    0 0 0 0       <NA>
6      b     bda      0    0 0 0 0       <NA>
7      b     dab      0    0 0 0 0       <NA>
8      d     abb      1    0 0 0 1         d
9      a     bbd      1    1 0 0 0         a
10     b     bda      1    0 1 0 0         b
11     c     dab      1    0 1 0 0         b
12     d     abc      0    0 0 0 0       <NA>

我希望有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

rollapply中的zoo功能可能会有所帮助。

定义原始data.frame和加载包:

library(zoo) 
df <- data.frame(value = c("a","b","b","d","a","b",
                           "b","d","a","b","c","d"), 
                 pattern = c("NA","a","ab","abb","bbd","bda",
                             "dab","abb","bbd","bda","dab","abc"))

定义一个函数,该函数将吐出第五个元素出现在前四个元素中的次数:

f <- function(x) sum(x[5] == x[1:4])

使用rollapply

应用此功能
df$appearance <- rollapply(df$pattern, 5, f, align = 'right', fill = NA)

我不确定我是否正确解释您的字母列,但您可以对单个字母使用相同(或类似)的函数,然后根据值列将结果列拆分为4。

df$letters <- rollapply(df$value, 5, f, align = 'right', fill = NA)
df$a <- 0
df$a[df$value == 'a'] <- df$letters[df$value == 'a']

由您决定如何在开始时处理NA值。

如果我猜一下,看起来你正在使用DNA密码子。如果你还没有这样做,你可能想看一下现有的包。特别是生物传导器有许多用于处理生物数据的有用物质。

答案 1 :(得分:0)

您可以使用此方法:

df <- data.frame(
        value = c("a","b","b","d","a","b","b","d","a","b","c","d"), 
        pattern = c(NA,"a","ab","abb","bbd","bda","dab","abb","bbd","bda","dab","abc"))

win <- 4
analyzeWindow <- function(idx){
  idxs <- max(1,idx-win):(idx-1)
  if(idx == 1) idxs <- integer()
  winDF <- df[idxs,]
  winDF <- winDF[na.omit(winDF$pattern == df$pattern[idx]),]
  expValWeights <- unlist(as.list(table(winDF$value)))

  c(appearances=nrow(winDF),expValWeights)
}

newCols <- t(sapply(1:nrow(df),analyzeWindow))
df2 <- cbind(df,newCols)
df2$exp.result <- colnames(newCols)[-1][max.col(newCols[,-1],ties.method='first')]
df2$exp.result[rowSums(newCols[,-1]) == 0] <- NA

> df2

   value pattern appearances a b c d exp.result
1      a    <NA>           0 0 0 0 0       <NA>
2      b       a           0 0 0 0 0       <NA>
3      b      ab           0 0 0 0 0       <NA>
4      d     abb           0 0 0 0 0       <NA>
5      a     bbd           0 0 0 0 0       <NA>
6      b     bda           0 0 0 0 0       <NA>
7      b     dab           0 0 0 0 0       <NA>
8      d     abb           1 0 0 0 1          d
9      a     bbd           1 1 0 0 0          a
10     b     bda           1 0 1 0 0          b
11     c     dab           1 0 1 0 0          b
12     d     abc           0 0 0 0 0       <NA>

注意: 此代码要求“value”列为类型因子。如果不是,请使用as.factor