1. 数据表
首先将问题中的奇怪构造替换为t
更常见的构造:
library(data.table)
t <- data.table(
Date = as.Date(c("2013-5-4", "2013-5-9", "2013-5-16", "2013-5-30")),
V1 = c(4, 2, 3, 1),
V2 = c(2, 5, 2, 4)
)
现在tabulate
每一行并用于cumsum
累积先前的行。 perm
是一个置换向量,用于重新排列 C 列 (nc + 1:n) 和 PC 列 (nc + n + 1:n) 的列号。
nc <- ncol(t) # 3
n <- t[, max(V1, V2)] # 5
Cnames <- paste0("C", 1:n)
PCnames <- paste0("PC", 1:n)
perm <- c(1:nc, rbind(nc + 1:n, nc + n + 1:n))
t[, (Cnames) := as.list(tabulate(c(V1, V2), n)), by = 1:nrow(t)
][, (Cnames):=lapply(.SD, function(x) cumsum(x) - x), .SDcol=Cnames
][, (PCnames):=lapply(.SD, function(x) x/seq(0,len=.N,by=nc-1)), .SDcols=Cnames
][, ..perm]
最后一行给出:
Date V1 V2 C1 PC1 C2 PC2 C3 PC3 C4 PC4 C5 PC5
1: 2013-05-04 4 2 0 NaN 0 NaN 0 NaN 0 NaN 0 NaN
2: 2013-05-09 2 5 0 0 1 0.5 0 0.0000000 1 0.5000000 0 0.0000000
3: 2013-05-16 3 2 0 0 2 0.5 0 0.0000000 1 0.2500000 1 0.2500000
4: 2013-05-30 1 4 0 0 3 0.5 1 0.1666667 1 0.1666667 1 0.1666667
1a.data.table 替代方案
如果可以省略第一个日期的行(这不是很有用,因为在第一个日期之前没有日期),那么我们可以执行以下乏味但直接的自连接:
t <- data.table(
Date = as.Date(c("2013-5-4", "2013-5-9", "2013-5-16", "2013-5-30")),
V1 = c(4, 2, 3, 1),
V2 = c(2, 5, 2, 4)
)
tt <- t[, one := 1]
setkey(tt, one)
tt[tt,,allow.cartesian=TRUE][Date > Date.1, list(
C1 = sum(.SD == 1), PC1 = mean(.SD == 1),
C2 = sum(.SD == 2), PC2 = mean(.SD == 2),
C3 = sum(.SD == 3), PC3 = mean(.SD == 3),
C4 = sum(.SD == 4), PC4 = mean(.SD == 4),
C5 = sum(.SD == 5), PC5 = mean(.SD == 5)
), by = list(Date, V1, V2), .SDcols = c("V1.1", "V2.1")]
1b。数据表替代
或者我们可以更紧凑地重写 1a (其中tt
、n
和来自上面):Cnames
PCnames
tt[tt,,allow.cartesian=TRUE][Date > Date.1, setNames(as.list(rbind(
sapply(1:n, function(i, .SD) sum(.SD==i), .SD=.SD),
sapply(1:n, function(i, .SD) mean(.SD==i), .SD=.SD)
)), c(rbind(Cnames, PCnames))),
by = list(Date, V1, V2), .SDcols = c("V1.1", "V2.1")]
2.sqldf
data.table 的替代方法是将 SQL 与这种同样乏味但直接的自联接一起使用:
library(sqldf)
sqldf("select a.Date, a.V1, a.V2,
sum(((b.V1 = 1) + (b.V2 = 1)) * (a.Date > b.Date)) C1,
sum(((b.V1 = 1) + (b.V2 = 1)) * (a.Date > b.Date)) /
cast (2 * count(*) - 2 as real) PC1,
sum(((b.V1 = 2) + (b.V2 = 2)) * (a.Date > b.Date)) C2,
sum(((b.V1 = 2) + (b.V2 = 2)) * (a.Date > b.Date)) /
cast (2 * count(*) - 2 as real) PC2,
sum(((b.V1 = 3) + (b.V2 = 3)) * (a.Date > b.Date)) C3,
sum(((b.V1 = 3) + (b.V2 = 3)) * (a.Date > b.Date)) /
cast (2 * count(*) - 2 as real) PC3,
sum(((b.V1 = 4) + (b.V2 = 4)) * (a.Date > b.Date)) C4,
sum(((b.V1 = 4) + (b.V2 = 4)) * (a.Date > b.Date)) /
cast (2 * count(*) - 2 as real) PC4,
sum(((b.V1 = 5) + (b.V2 = 5)) * (a.Date > b.Date)) C5,
sum(((b.V1 = 5) + (b.V2 = 5)) * (a.Date > b.Date)) /
cast (2 * count(*) - 2 as real) PC5
from t a, t b where a.Date >= b.Date
group by a.Date")
2a. sqldf 替代方案
另一种方法是使用字符串操作来创建上面的 sql 字符串,如下所示:
f <- function(i) {
s <- fn$identity("sum(((b.V1 = $i) + (b.V2 = $i)) * (a.Date > b.Date))")
fn$identity("$s C$i,\n $s /\ncast (2 * count(*) - 2 as real) PC$i")
}
s <- fn$identity("select a.Date, a.V1, a.V2, `toString(sapply(1:5, f))`
from t a, t b where a.Date >= b.Date
group by a.Date")
sqldf(s)
2b。第二个 sqldf 替代方案
如果我们愿意在第一个日期没有输出行,则可以大大简化 sql 解决方案。这可能是有道理的,因为第一个日期没有以前的日期可以制表:
sqldf("select a.Date, a.V1, a.V2,
sum((b.V1 = 1) + (b.V2 = 1)) C1,
avg((b.V1 = 1) + (b.V2 = 1)) PC1,
sum((b.V1 = 2) + (b.V2 = 2)) C2,
avg((b.V1 = 2) + (b.V2 = 2)) PC2,
sum((b.V1 = 3) + (b.V2 = 3)) C3,
avg((b.V1 = 3) + (b.V2 = 3)) PC3,
sum((b.V1 = 4) + (b.V2 = 4)) C4,
avg((b.V1 = 4) + (b.V2 = 4)) PC4,
sum((b.V1 = 5) + (b.V2 = 5)) C5,
avg((b.V1 = 5) + (b.V2 = 5)) PC5
from t a, t b where a.Date > b.Date
group by a.Date")
同样,可以创建 sql 字符串以避免以与先前解决方案中所示相同的方式重复。
更新:添加了 PC 列和一些简化
更新 2:添加了额外的解决方案