如何将数据从宽格式融化为长格式但日期正确
How to melt data from wide to long format but with correct dates
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")
但它在几个月内没有给我正确的输出。
使用 good-old 基础 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
我们还可以在执行上面的 lapply
步骤后使用 data.table
melt
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
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")
但它在几个月内没有给我正确的输出。
使用 good-old 基础 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
我们还可以在执行上面的 lapply
步骤后使用 data.table
melt
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