根据R中的日期变量重新组织多个变量

问题描述

如果我有一个数据集,该数据集包含在不同时间点收集的相同度量的分数,我该如何组织这些日期/时间,以便它们代表某个日期之后的时间点?这是否可以在 R 中执行,或者我在另一个程序中执行此操作会更容易吗?

我有一个目前看起来像这样的数据集:

id  date        score1_date score1  score2_date score2  score3_date score3
101 1/6/2020    1/1/2020    20      1/8/2020    18      1/15/2020   16
102 2/27/2020   2/14/2020   16      2/21/2020   16      2/28/2020   10
103 1/10/2020   1/7/2020    30      1/14/2020   25      1/21/2020   20
104 3/5/2020    3/6/2020    40      3/13/2020   42      3/20/2020   40

我想找到与 [date] 最接近的 [score#_date] 并将其标识为 [time1],然后将其后的所有内容作为 [time2]、[time3] 等。

这是上表的代码:

structure(list(id = c(101,102,103,104),date = structure(c(18267,18319,18271,18326),class = "Date"),score1_date = structure(c(18262,18306,18268,18327),score1 = c(20,16,30,40),score2_date = structure(c(18269,18313,18275,18334),score2 = c(18,25,42),score3_date = structure(c(18276,18320,18282,18341),score3 = c(16,10,20,40)),row.names = c(NA,-4L),class = c("tbl_df","tbl","data.frame"))

所以我最终希望数据集具有如下所示的变量:

id  date        time1_date  time1_score time2_date  time2_score time3_date  time3_score
101 1/6/2020    1/8/2020    18          1/15/2020   16          NA          NA
102 2/27/2020   2/28/2020   10          NA          NA          NA          NA
103 1/10/2020   1/7/2020    30          1/14/2020   25          1/21/2020   20
104 3/5/2020    3/6/2020    40          3/13/2020   42          3/20/2020   40

非常感谢!

解决方法

使用 func(Math.random() < 0.5 ? "a" : "b"); // okay // function func3<"b">(f: "a" | "b"): "a" | "b" func("a") // okay // function func<"a">(f: "a"): "a" func(Math.random() < 0.5 ? "a" : "d"); // fails func("c") // okay! // function func<"c"> (f: "a" | "c"): "a" | "c" 函数你可以:

"c"
,

这是一种稍微不同的方法,包括对最后一列进行重新排序:

"wb"
,

我建议使用不同的策略来过滤行,如下所示使用 cumminabs 函数

df %>%
  rename(base_date = date) %>%
  rename_with(~paste0(.,'_value'),matches('^score\\d+$')) %>%
  pivot_longer(cols = starts_with('score'),names_to = c('score','.value'),names_sep = '_') %>%
  group_by(id) %>%
  mutate(d = abs(as.numeric(difftime(base_date,date,units = "days"))),score = str_remove(score,"\\d$")) %>%
  filter(cumsum(cummin(d) == min(d)) != 0) %>%
  mutate(score = paste0(score,row_number())) %>%
  pivot_wider(id_cols = c(id,base_date),names_from = score,values_from = c(date,value))

# A tibble: 4 x 8
# Groups:   id [4]
     id base_date  date_score1 date_score2 date_score3 value_score1 value_score2 value_score3
  <dbl> <date>     <date>      <date>      <date>             <dbl>        <dbl>        <dbl>
1   101 2020-01-06 2020-01-08  2020-01-15  NA                    18           16           NA
2   102 2020-02-27 2020-02-28  NA          NA                    10           NA           NA
3   103 2020-01-10 2020-01-07  2020-01-14  2020-01-21            30           25           20
4   104 2020-03-05 2020-03-06  2020-03-13  2020-03-20            40           42           40

pivot_wider 有一个列重新排列 issue #839 在 Github 上仍然开放,所以你可以手动重新排列列或采用 Ian 在他的 answer 的最后一行提出的策略

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...