问题描述
我正在尝试从长到宽重塑数据,但是在这里我需要创建名称列,例如event1,event2,event3等。换句话说,对于 names_from没有自然的候选人
参数。我尝试了几种不同的方法,但无法获得所需的内容-这是一个可重复的示例。
I'm trying to reshape my data from long to wide, but here I need to create names column such as event1, event2, event3, etc. In other words, there's no natural candidate for names_from
argument. I've tried a couple of different ways but cannot get what I'm looking for- Here's a reproducible example.
set.seed(57)
df <- data.frame(date = seq.Date(as.Date("2009-01-01"), as.Date("2009-01-12"), by = 1),
id = rep(1:3, each = 4),
val = rnorm(12)) %>% filter(val > 0.5)
我想将 df
转换为 df2
。
df2 <- data.frame(id = c(1:3),
event1 = c("2009-01-03", "2009-01-06", "2009-01-10"),
event2 = c("2009-01-04", "2009-01-07", "2009-01-11"),
event3 = c(" ", " ", "2009-01-12"))
请注意,此数据集记录了每个id的第一个,第二个和第三个(如果有的话)发生日期。
Note that this data set documents the first, second, and third (if any) occurrence date for each id.
已经尝试使用 names_prefix
,但似乎不起作用。
Here's what I've tried using names_prefix
but doesn't seem to work.
set.seed(57)
df <- data.frame(date = seq.Date(as.Date("2009-01-01"), as.Date("2009-01-12"), by = 1),
id = rep(1:3, each = 4),
val = rnorm(12)) %>% filter(val > 0.5) %>%
pivot_wider(names_prefix = "event", names_from = val, values_from = date)
推荐答案
我们可以创建一个新的id列,然后使用 pivot_wider
。
We could create a new id column and then use pivot_wider
.
library(dplyr)
df %>%
group_by(id) %>%
mutate(row = row_number()) %>%
select(-val) %>%
tidyr::pivot_wider(names_from = row, values_from = date, names_prefix = 'event')
# id event1 event2 event3
# <int> <date> <date> <date>
#1 1 2009-01-03 2009-01-04 NA
#2 2 2009-01-06 2009-01-07 NA
#3 3 2009-01-10 2009-01-11 2009-01-12
使用 data.table
:
library(data.table)
dcast(setDT(df), id~rowid(id), value.var = 'date')
这篇关于没有名称列时(或应创建名称列时)pivot_wider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!