如何在一段时间内将唯一标识符的位置从多行转换为单行?

问题描述 投票:0回答:1

我需要有关唯一查询的帮助,我目前找不到任何解决方案。但这可能是因为我不知道该如何表达自己的看法。以下是样本数据集

name, position, start_date, end_date
ABC, Contractor, 09/02/2017, 07/01/2018
ABC, Associate Consultant, 08/01/2018, 31/12/2018
ABC, Consultant, 01/01/2019, 31/05/2019

从本质上说,ABC是随着时间的推移而担任不同职务的人。我想转换此数据集,以便将ABC放置在单行中,并随时间跟踪其位置。 。Attached is an image that displays the solution I am looking for

非常感谢您的帮助!

最好,罗希特

r date transform
1个回答
0
投票
library(dplyr)
library(tidyr)
df_txt <- c("name, position, start_date, end_date
ABC, Contractor, 09/02/2017, 07/01/2018
ABC, Associate Consultant, 08/01/2018, 31/12/2018
ABC, Consultant, 01/01/2019, 31/05/2019")

read_csv(df_txt) %>% 
  mutate_at(dplyr::vars(ends_with("_date")), .funs = dmy) %>% 
  rowwise() %>% 
  mutate(interval = list(seq(start_date, end_date, by ="month"))) %>% 
  select(name, position, interval) %>% 
  unnest(interval) %>% 
  mutate(interval = format(interval, "%Y-%m")) %>% 
  pivot_wider(id_cols = name, names_from = interval, values_from = position) %>% 
  view()
© www.soinside.com 2019 - 2024. All rights reserved.