2017-08-04 73 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)