我正在寻找一种在每个观察值旁边添加列标题(日期)的方法。
以df
:
structure(list(dates = c("wt", "id", "", ""), X6.1.2018 = c("dd",
"a", "b", "c"), X6.2.2018 = c("qq", "d", "e", ""), X6.2.2018.1 = c("dd",
"z", "y", "")), class = "data.frame", row.names = c(NA, -4L))
df
如下:
dates 6/1/2018 6/2/2018 6/2/2018
wt dd qq dd
id a d z
b e y
c
我想以df_final
结尾:
id date
a 6/1/2018
b 6/1/2018
c 6/1/2018
d 6/2/2018
e 6/2/2018
z 6/2/2018
y 6/2/2018
任何想法都很有帮助-谢谢
答案 0 :(得分:1)
使用tidyverse
:
library(tidyverse)
df %>%
filter(dates != 'wt') %>%
select(-dates) %>%
gather(date, id) %>%
filter(id != '') %>%
mutate(date = as.Date(date, format = "X%m.%d.%Y"))
输出:
date id
1 2018-06-01 a
2 2018-06-01 b
3 2018-06-01 c
4 2018-06-02 d
5 2018-06-02 e
6 2018-06-02 z
7 2018-06-02 y
或使用data.table::melt
:
library(data.table)
dt = setDT(df)[dates != 'wt', !'dates']
melt(dt, measure.vars = 1:3, variable.name = "date",
value.name = "id")[id != '', .(id, date = as.Date(date, format = "X%m.%d.%Y"))]
输出:
id date
1: a 2018-06-01
2: b 2018-06-01
3: c 2018-06-01
4: d 2018-06-02
5: e 2018-06-02
6: z 2018-06-02
7: y 2018-06-02