融化数据框,重塑一个高大的数据框

Melt the data frame, reshape a tall data frame

我有以下数据框 (df),但我认为我无法全神贯注于如何执行以下操作:

输入:

id  business_id type                      date1     date2     date3
1   A1          Month                     13/10/13  13/09/13  13/08/13
1   A1          Total Net Deposits        1500      951       190
1   A1          Month end Bank Balance    729       650       164

预期输出:

id  business_id Month       Total Net Deposits  Month end Bank Balance 
1   A1          13/10/13    1500                729 
1   A1          13/09/13    951                 650
1   A1          13/09/13    190                 164

这里有一个 tidyr 选项:

library(tidyr)
df %>% 
  gather(date, val, date1:date3) %>% 
  spread(key = type, val = val)
#  id business_id  date    Month Month end Bank Balance Total Net Deposits
#1  1          A1 date1 13/10/13                    729               1500
#2  1          A1 date2 13/09/13                    650                951
#3  1          A1 date3 13/08/13                    164                190
#Warning:
#attributes are not identical across measure variables; they will be dropped 

如果您的列存储为因子,则会出现警告,但您可以忽略它。

您可以对基础 R(统计信息)和其他一些库中的 reshape2data.tablereshape 执行相同的操作。

library(reshape2)
df.m<-melt(x,id.var=c("id", "business_id","type"))
dcast(df.m,id+business_id+variable~type)

如果需要,您可以删除变量 "variable"。

  id business_id variable    Month MonthendBankBalance TotalNetDeposits
1  1          A1    date1 13/10/13                 729             1500
2  1          A1    date2 13/09/13                 650              951
3  1          A1    date3 13/08/13                 164              190

我们可以使用 base R

cbind(df1[1:2],setNames(as.data.frame(t(df1[-(1:3)])), df1$type))
#      id business_id    Month Total Net Deposits Month end Bank Balance
#date1  1          A1 13/10/13               1500                    729
#date2  1          A1 13/09/13                951                    650
#date3  1          A1 13/08/13                190                    164