2017-08-16 61 views
0

1)我有一個名爲「UBER」的事務文件
2)我也有一個「Target」df,我需要匹配的總數我的「UBER」總計
3)我需要找到目標和UBER(間〜目標的差異 - UBER)
4),然後我會疊加到尤伯杯那麼這將使得UBER =目標如何使用dplyr :: nest來計算兩個數據集之間的差異

數據的差異對於UBER-交易文件

UBER <- data.frame(
    Cube = c("UBER", "UBER", "UBER", "UBER"), 
Country = c("AU", "AU", "AU", "AU"), 
Product = c("0001", "0001", "0001", "0001"), 
    LOB = c("xxx", "xxx", "xxx", "xxx"), 
    Mth_Yr = 1:4, 
Revenue = c(5, 5, 5, 5) 
    ) 

目標df茹d是

Target <- data.frame(
    Cube = c("Target", "Target", "Target", "Target"), 
Country = c("AU", "AU", "AU", "AU"), 
Product = c("0001", "0001", "0001", "0001"), 
    LOB = c("xxx", "xxx", "xxx", "xxx"), 
    Mth_Yr = 1:4, 
Revenue = c(10, 10, 10, 10) 
    ) 

我建立了一個功能來幫我分析:

target_less_uber <- function(df){ 

    target <- df %>% #as.data.frame() %>% 
     filter(Cube == "Target") %>% 
     select(Revenue) %>% as.numeric() 

    uber <- df %>% #as.data.frame() %>% 
     filter(Cube == "UBER") %>% 
     select(Revenue) %>% as.numeric()  

    output <- data.frame(Overlay = target - uber) 

    return(output) 

}

這是我嘗試過 - 這是不工作

library(dplyr) 
 
library(purrr) 
 

 
ALL <- rbind(Target, UBER) 
 
ALL %>% 
 
     nest(Cube, Revenue) %>% 
 
     transmute(Country, Product, LOB, Mth_Yr, res = map(data, target_less_uber) %>% unnest

任何幫助意見,高度讚賞。

+0

爲了確認,你希望兩個數據集相匹配的所有四個變量(國家,產品,LOB,Mth_Yr)?每個數據集中只有一行是針對這四個變量的每個組合的? –

+0

@ Z.Lin是的,這是正確的 – cephalopod

回答

1

我不是很熟悉的nest()命令,所以我不能有解決你的代碼,但我下面的工作:

inner_join(UBER %>% select(-Cube) %>% rename(UBER = Revenue), 
      Target %>% select(-Cube) %>% rename(Target = Revenue)) %>% 
    mutate(overlay = Target - UBER) %>% 
    select(-UBER, -Target) 
+0

這是一個優雅和簡單的答案。簡化我的代碼非常多。謝謝,非常感謝。 – cephalopod

相關問題