1

我有以下格式的大量事件列表:

> dput(head(events))
structure(list(action = c("110:0.49,258:0.49", "110:0.49,258:0.49", 
"110:0.49,258:0.49", "114:1.0,299:1.0", "114:1.0,299:1.0", "110:0.49"
), response = c("113=5-110=266-111=30-258=248-99=18-264=15", "113=5-110=278-111=30-258=260-99=18-264=15", 
"113=5-110=284-111=30-258=266-99=18-264=15", "114=34-299=34-108=134-110=12-246=67", 
"114=34-299=34-108=134-110=18-246=67", "114=34-113=6-299=34-108=146-110=24-246=73"
)), .Names = c("action", "response"), row.names = c(NA, 6L), class = "data.frame")

两者actionresponse都是从 和 之类的键映射到110和之类的114值。0.495

我想要的是一个矩阵,其(i,j)条目sum(action[i] * response[j])涵盖所有事件,其中action[i]是键的值i(对于 也是类似的response)。此外,我需要向量sum(action[i])sum(response[j]).

我可以使用这样的东西来做到这一点:

# split actions
l <- strsplit(events$action,",")
ll <- sapply(l,length)
l <- unlist(l)
l1 <- strsplit(l,":")
rm(l)
df1 <- data.frame(response = events$response[rep(1:nrow(events), ll)],
                  action = as.factor(sapply(l1,"[[",1)),
                  action.weight = as.numeric(sapply(l1,"[[",2)))

# split responses
l <- strsplit(df1$response,"-")
ll <- sapply(l,length)
l <- unlist(l)
l1 <- strsplit(l,"=")
rm(l)
rows <- rep(1:nrow(df1), ll)
df2 <- data.frame(action = df1$action[rows],
                  action.weight = df1$action.weight[rows],
                  response = as.factor(sapply(l1,"[[",1)),
                  response.weight = as.numeric(sapply(l1,"[[",2)))
df2$weight <- df2$action.weight * df2$response.weight
df2$action.weight <- NULL
df2$response.weight <- NULL

# summarise by action/response
dt1 <- as.data.table(df2)
setkeyv(dt1,c("action","response"))
dt2 <- dt1[, sum(weight), by="action,response"]

我认为这或多或少应该是我需要的。

但是,中间对象 ( df1, df2, l, &c) 对于我的 RAM 来说太大了。我想知道是否有办法以更有效的方式完成我需要的事情。

PS。action和的键集response实际上是相同的,但似乎没有理由依赖这一点。

4

0 回答 0