如何将数据从宽格式转换为长格式但日期正确

时间:2019-05-07 07:37:34

标签: r reshape

ID  Date1        Date2       Date3        Value1  Value2  Value3
1   1/14/2019    4/14/2019   8/14/2019     123     122     143  
2   1/14/2019    4/14/2019   8/14/2019     111     116     119  

我需要将上面的数据框转换为R中的下面的数据

预期产量

ID   Date     Value
1    January  123   
1    April    122
1    August   143
2    January  111   
2    April    116
2    August   119

我尝试过的事情:

library(reshape2)
long <- melt(wide, id.vars = "ID") 

但几个月后却无法给我适当的输出。

1 个答案:

答案 0 :(得分:2)

使用陈旧的基数R reshape

cols <- grep("^Date", names(df))

df[cols] <- lapply(df[cols], function(x) format(as.Date(x, "%m/%d/%Y"), "%B"))

reshape(df, timevar = "ID", direction = "long", 
  varying = list(cols, grep("Value", names(df))), v.names = c("Date", "Value"))

#    ID    Date Value id
#1.1  1 January   123  1
#2.1  1 January   111  2
#1.2  2   April   122  1
#2.2  2   April   116  2
#1.3  3  August   143  1
#2.3  3  August   119  2

从上方执行data.table步骤后,我们也可以使用melt lapply

library(data.table)

melt(setDT(df), id="ID", measure=patterns("^Date", "^Value"),
               value.name=c("Date", "Value"))

或者使用tidyverse

沿一条链进行所有操作
library(tidyverse)
library(lubridate)

df %>%
  gather(key, value, -ID) %>%
  group_by(group = sub("\\d+", "", key)) %>%
  select(-key) %>%
  mutate(row =row_number()) %>%
  spread(group, value) %>%
  mutate(Date = format(mdy(Date), "%B")) %>%
  select(-row)

#     ID Date    Value
#   <int> <chr>   <chr>
#1     1 January 123  
#2     1 April   122  
#3     1 August  143  
#4     2 January 111  
#5     2 April   116  
#6     2 August  119