传递带有重复标识符的其他列

时间:2017-11-10 02:27:29

标签: r dplyr tidyverse spread

我通过传递spread获得了feature_code actv_amt以下数据框,以便为相应的{{1}获取相应的actv_amt代码。我试图以feature传递它传递的值但不合并数据。

作为参考,我先前问过一个问题 take unique count and sum each unique values in R

count_FEATURE = ACTV_AMT

输入类型2:

Input type: 1
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_CODE    L_NU    
7/27/16 7/27/16 265       O          15          1      INTEREST        855          
7/27/16 7/27/16 265       O          14          1      INTEREST        855 

getting Output
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_INTEREST     L_NU   
7/27/16 7/27/16 265      O           29          1             2             855

Expected output:
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_INTEREST     L_NU   
7/27/16 7/27/16 265      O           29          1             29             855

实施代码:

Input
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_CODE    L_NU    
7/27/16 7/27/16 265            O          15       1     INTEREST        855          
7/27/16 7/27/16 265            O          14       1     INSTALLMENT   855    

Getting output:
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   INTEREST INSTALLMENT     L_NU   
7/Expected7/16 265          O           29           1      1          1           855 

Expected output:
ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   INTEREST INSTALLMENT     L_NU   
7/27/16 7/27/16 265        O           29           1      15         14           855 

任何人都可以帮助我获得预期的输出。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样

library(reshape2)

df <- read.table(text = "ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_CODE    L_NU    
7/27/16 7/27/16 265       O          15          1      INTEREST        855          
7/27/16 7/27/16 265       O          14          1      INTEREST        855", header = T)

dcast(df, ST_DATE+ND_DATE+LO_NO+ACTV_CODE+AB_NO+L_NU~FEATURE_CODE, value.var = "ACTV_AMT", fun.aggregate = sum)

output:
-------
  ST_DATE ND_DATE LO_NO ACTV_CODE AB_NO L_NU INTEREST
1 7/27/16 7/27/16   265         O     1  855       29

input2:
-------
df <- read.table(text = "ST_DATE ND_DATE LO_NO   ACTV_CODE   ACTV_AMT    AB_NO   FEATURE_CODE    L_NU    
7/27/16 7/27/16 265            O          15       1     INTEREST        855          
7/27/16 7/27/16 265            O          14       1     INSTALLMENT   855", header = T)

dcast(df, ST_DATE+ND_DATE+LO_NO+ACTV_CODE+AB_NO+L_NU~FEATURE_CODE, value.var = "ACTV_AMT", fun.aggregate = sum)

output:
-------
  ST_DATE ND_DATE LO_NO ACTV_CODE AB_NO L_NU INSTALLMENT INTEREST
1 7/27/16 7/27/16   265         O     1  855          14       15