【发布时间】:2022-02-10 22:14:21
【问题描述】:
我在三个时间点测试了参与者。我有他们测试的日期。我想创建一个级别为第一、第二和第三的列。每个参与者都有三个日期,因此每个参与者的日期都不同。数据如下所示:
structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), time_tested = c("2022-02-05", "2022-02-05", "2022-02-05",
"2022-02-08", "2022-02-08", "2022-02-08", "2022-02-11", "2022-02-11",
"2022-02-11", "2022-02-08", "2022-02-08", "2022-02-08", "2022-02-10",
"2022-02-10", "2022-02-10", "2022-02-13", "2022-02-13", "2022-02-13",
"2022-02-05", "2022-02-05", "2022-02-05", "2022-02-08", "2022-02-08",
"2022-02-08", "2022-02-11", "2022-02-11", "2022-02-11")), class = "data.frame", row.names = c(NA,
-27L))
这就是我想要的结果:
structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), time_tested = c("2022-02-05", "2022-02-05", "2022-02-05",
"2022-02-08", "2022-02-08", "2022-02-08", "2022-02-11", "2022-02-11",
"2022-02-11", "2022-02-08", "2022-02-08", "2022-02-08", "2022-02-10",
"2022-02-10", "2022-02-10", "2022-02-13", "2022-02-13", "2022-02-13",
"2022-02-05", "2022-02-05", "2022-02-05", "2022-02-08", "2022-02-08",
"2022-02-08", "2022-02-11", "2022-02-11", "2022-02-11"), period = c("first",
"first", "first", "second", "second", "second", "third", "third",
"third", "first", "first", "first", "second", "second", "second",
"third", "third", "third", "first", "first", "first", "second",
"second", "second", "third", "third", "third")), class = "data.frame", row.names = c(NA,
-27L))
谢谢!
【问题讨论】:
-
请不要使用数据图像,因为没有大量不必要的努力就无法使用它们。 For multiple reasons 问题应该是可重复的。这使得其他可能想要帮助的人轻松复制数据。查看堆栈溢出指南minimal reproducible example 和How to Ask。以对象的形式包含一个最小数据集,例如,如果一个数据框为
df <- data.frame(…),其中……是您的变量和值,或者使用dput(head(df))。 Good overview on asking questions