2017-08-04 116 views
1

我有两个dataframes:df_workingFile和df_groupIDs快速方式

df_workingFile:

ID | GroupID | Sales | Date 
v | a1  | 1 | 2011 
w | a1  | 3 | 2010 
x | b1  | 8 | 2007 
y | b1  | 3 | 2006 
z | c3  | 2 | 2006 

df_groupIDs:

GroupID | numIDs | MaxSales 
a1  | 2  | 3  
b1  | 2  | 8  
c3  | 1  | 2  

对于df_groupIDs,我想获取该组中最高销售额的活动的ID和日期。所以组“a1”在df_workingFile,“v”和“w”中有2个事件。我想确定事件“w”具有最大销售价值并将其信息带入df_groupIDs。最终的输出应该是这样的:

GroupID | numIDs | MaxSales | ID | Date 
a1  | 2  | 3  | w | 2010 
b1  | 2  | 8  | x | 2007 
c3  | 1  | 2  | z | 2006 

现在,这里的问题。我编写了这样的代码,但是当我处理50-100K行数据集时,它效率非常低,需要永久处理。我需要帮助弄清楚如何重写我的代码以提高效率。这是我目前有:

i = 1 
for (groupID in df_groupIDs$groupID) { 

    groupEvents <- subset(df_workingFile, df_workingFile$groupID == groupID) 
    index <- match(df_groupIDs$maxSales[i], groupEvents$Sales) 
    df_groupIDs$ID[i] = groupEvents$ID[index] 
    df_groupIDs$Date[i] = groupEvents$Date[index] 

    i = i+1 
} 

回答

4

使用dplyr

library(dplyr) 

df_workingFile %>% 
    group_by(GroupID) %>%  # for each group id 
    arrange(desc(Sales)) %>% # sort by Sales (descending) 
    slice(1) %>%    # keep the top row 
    inner_join(df_groupIDs) # join to df_groupIDs 
    select(GroupID, numIDs, MaxSales, ID, Date) 
    # keep the columns you want in the order you want 

另一种更简单的方法,如果Sales是整数(并因此可以平等与MaxSales列测试依赖于):

inner_join(df_groupIDs, df_workingFile, 
      by = c("GroupID" = "GroupID", "MaxSales" = "Sales")) 
1

这使用SQLite具有的功能,如果使用max一条线然后自动带来最大来自的行。

library(sqldf) 

sqldf("select g.GroupID, g.numIDs, max(w.Sales) MaxSales, w.ID, w.Date 
     from df_groupIDs g left join df_workingFile w using(GroupID) 
     group by GroupID") 

,并提供:

GroupID numIDs MaxSales ID Date 
1  a1  2  3 w 2010 
2  b1  2  8 x 2007 
3  c3  1  2 z 2006 

注:重复地示出两个输入数据帧是:

Lines1 <- " 
ID | GroupID | Sales | Date 
v | a1  | 1 | 2011 
w | a1  | 3 | 2010 
x | b1  | 8 | 2007 
y | b1  | 3 | 2006 
z | c3  | 2 | 2006" 
df_workingFile <- read.table(text = Lines1, header = TRUE, sep = "|", strip.white = TRUE) 

Lines2 <- " 
GroupID | numIDs | MaxSales 
a1  | 2  | 3  
b1  | 2  | 8  
c3  | 1  | 2"  

df_groupIDs <- read.table(text = Lines2, header = TRUE, sep = "|", strip.white = TRUE)