我想探索一个data.table分组的优雅(单行)解决方案
我有一个data.table如下:
library(data.table) library(lubridate) dt.master <- data.table(user = c(1000, 1002, 2008, 3005, 1000, 1002, 1002), target = c(50000, 50004, 50501, 50001, 50000, 50000, 50004), channel = c("A", "B", "C", "A", "B", "A", "C"), date = c(dmy("10/02/2018"), dmy("11/04/2018"), dmy("14/03/2018"), dmy("02/03/2018"), dmy("05/01/2018"), dmy("08/05/2018"), dmy("05/03/2018")))那是:
user target channel date 1: 1000 50000 A 2018-02-10 2: 1002 50004 B 2018-04-11 3: 2008 50501 C 2018-03-14 4: 3005 50001 A 2018-03-02 5: 1000 50000 B 2018-01-05 6: 1002 50000 A 2018-05-08 7: 1002 50004 C 2018-03-05我想知道,对于每个(user, target)组的第一次出现的频道,并将其添加到dt.master。 这是:
user target channel date first_channel 1: 1000 50000 A 2018-02-10 B 2: 1000 50000 B 2018-01-05 B 3: 1002 50000 A 2018-05-08 A 4: 1002 50004 B 2018-04-11 C 5: 1002 50004 C 2018-03-05 C 6: 2008 50501 C 2018-03-14 C 7: 3005 50001 A 2018-03-02 A目前,我正在分两步进行:
首先,我提取第一次发生的行
dt.result <- dt.master[dt.master[, .(first_interest = .I[which.min(date)]), by = c("user", "target")]$first_interest,]之后,我将它与dt.master合并
setnames(dt.result, "channel", "first_channel") dt.master <- merge(dt.master, dt.result[, .(user, target, first_channel)], by.x = c("user", "target"), by.y = c("user", "target"), all.x = T, all.y = F)有没有办法做到没有合并? 我相信必须有修改第一行的解决方案,但我找不到它。
非常感谢!
I would like to explore an elegant (one-liner) solution for a data.table grouping
I have a data.table as follows:
library(data.table) library(lubridate) dt.master <- data.table(user = c(1000, 1002, 2008, 3005, 1000, 1002, 1002), target = c(50000, 50004, 50501, 50001, 50000, 50000, 50004), channel = c("A", "B", "C", "A", "B", "A", "C"), date = c(dmy("10/02/2018"), dmy("11/04/2018"), dmy("14/03/2018"), dmy("02/03/2018"), dmy("05/01/2018"), dmy("08/05/2018"), dmy("05/03/2018")))That is:
user target channel date 1: 1000 50000 A 2018-02-10 2: 1002 50004 B 2018-04-11 3: 2008 50501 C 2018-03-14 4: 3005 50001 A 2018-03-02 5: 1000 50000 B 2018-01-05 6: 1002 50000 A 2018-05-08 7: 1002 50004 C 2018-03-05I would like to know, for each group of (user, target), the channel of the first ocurrence, and add it to the dt.master. This is:
user target channel date first_channel 1: 1000 50000 A 2018-02-10 B 2: 1000 50000 B 2018-01-05 B 3: 1002 50000 A 2018-05-08 A 4: 1002 50004 B 2018-04-11 C 5: 1002 50004 C 2018-03-05 C 6: 2008 50501 C 2018-03-14 C 7: 3005 50001 A 2018-03-02 ACurrently, I am doing it in two steps:
First, I extract the rows of the first ocurrence
dt.result <- dt.master[dt.master[, .(first_interest = .I[which.min(date)]), by = c("user", "target")]$first_interest,]And, afterwards, I merge it with dt.master
setnames(dt.result, "channel", "first_channel") dt.master <- merge(dt.master, dt.result[, .(user, target, first_channel)], by.x = c("user", "target"), by.y = c("user", "target"), all.x = T, all.y = F)Is there a way to do it with no merges? I believe there must be a solution modifying the first line, but I cannot find it.
Thanks a lot!
最满意答案
您可以按照以下参考组进行更新:
dt.master[, first_channel := channel[which.min(date)], keyby=.(user, target)]You can update by reference by groups as follows:
dt.master[, first_channel := channel[which.min(date)], keyby=.(user, target)]更多推荐
发布评论