R- Unpivot双列

时间:2018-05-11 19:40:11

标签: r unpivot

我有一个这样的数据集(是的,有双重标题,因为这些是我的BI工具必需的):

library(readxl)
df = read_excel("df.xlsx")
View(df)
Firm November November December December etc etc
Firm On-time  Pieces   On-time  Pieces   etc etc
A    37%      60       50%      10
B    60%      50       10%      55

我更喜欢以下格式:

Firm Month    On-time Pieces
A    December 50%     10
A    November 37%     60
Etc
etc

我试过了:

df %>%
gather(month, ot, -firm) %>%
filter(firm != "firm") %>%
arrange(firm)

但它没有给我我想要的东西。上面的功能给了我准时和分成一列,混合起来。

我无法在任何地方找到这个,但如果您找到了,请发表评论,我当然会删除该帖子。

提前致谢

1 个答案:

答案 0 :(得分:2)

假设您可以将双行标题减少到一行,以便您拥有以下格式的数据框...

df
  Firm Nov-OnTime Nov-Pieces Dec-OnTime Dec-Pieces
1    A        37%         60        50%         10
2    B        60%         50        10%         55

然后您可以使用gather - separate - spread序列来制作您想要的内容......

library (tidyr)
df2 <- df %>% gather(key=key, value=value, -Firm) %>%       #gather all columns except Firm
    separate(key, into=c("Month", "Type"), remove=TRUE) %>% #split into month and type
    spread(key=Type, value=value)                           #spread by Type (keeping month)

df2
  Firm Month OnTime Pieces
1    A   Dec    50%     10
2    A   Nov    37%     60
3    B   Dec    10%     55
4    B   Nov    60%     50