计算 R 中多个列中字符串的先前出现次数

Count previous occurences of a string across multiple columns in R

我有一个带有时间索引的四列矩阵和三列名称(字符串)。这是一些玩具数据:

x = rbind(c(1,"sam","harry","joe"), c(2,"joe","sam","jack"),c(3,"jack","joe","jill"),c(4,"harry","jill","joe"))

我想创建三个额外的向量来计算(对于每一行)该名称之前(但不是后续)出现的次数。这是玩具数据的预期结果:

y = rbind(c(0,0,0),c(1,1,0),c(1,2,0),c(1,1,3))

我不知道如何解决这个问题,并在 Stack Overflow 上搜索了相关示例。 dplyr 提供了查找总计数的答案,但(据我所知)不是逐行提供的。

我试图编写一个函数来处理单列中的这个问题space,但没有成功,即

thing = sapply(x,function(i)length(grep(i,x[x[1:i]])))

如有任何提示,我们将不胜感激。

这是一个典型的ave + seq_along类型的问题,但我们需要先将数据转换为向量:

t(`dim<-`(ave(rep(1, prod(dim(x[, -1]))), 
              c(t(x[, -1])), FUN = seq_along)  - 1, 
          rev(dim(x[, -1]))))
#      [,1] [,2] [,3]
# [1,]    0    0    0
# [2,]    1    1    0
# [3,]    1    2    0
# [4,]    1    1    3

也许更具可读性:

## x without the first column as a vector
x_vec <- c(t(x[, -1]))

## The values that you are looking to obtain...
y_vals <- ave(rep(1, length(x_vec)), x_vec, FUN = seq_along) - 1

## ... in the format you want to obtain them
matrix(y_vals, ncol = ncol(x) - 1, byrow = TRUE)
#      [,1] [,2] [,3]
# [1,]    0    0    0
# [2,]    1    1    0
# [3,]    1    2    0
# [4,]    1    1    3

你可以这样做:

el = unique(c(x[,-1]))
val = Reduce(`+`, lapply(el, function(u) {b=c(t(x[,-1]))==u; b[b==T]=(cumsum(b[b==1])-1); b}))

matrix(val, ncol=ncol(x[,-1]), byrow=T)
#         [,1] [,2] [,3]
#[1,]    0    0    0
#[2,]    1    1    0
#[3,]    1    2    0
#[4,]    1    1    3