我很困惑。我有一个看起来像这样的数据集:
dataset <- data.frame(
Label = c(1.1,1.1,1.1,2.1,2.1,2.1,3.1,3.1,3.1,1.6,1.6,1.6,2.6,2.6,2.6,3.6,3.6,3.6),
StudyID = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
ScanNumber = c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
Timepoint = c(1,1,1,1,1,1,1,1,1,6,6,6,6,6,6,6,6,6),
Fat = c(3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8),
Lean = c(5,5,5,6,6,6,7,7,7,3,3,3,4,4,4,5,5,5)
)
我想pivot_wider,以便对每个StudyID和时间点进行三倍的肥胖和瘦身测量。您可以看到Label包含有关StudyID和Timepoint的信息(例如,StudyID = 1且Timepoint = 6,Label为1.6)。这就是我的做法:
newdataset <- dataset %>%
pivot_wider(
id_cols = Label,
names_from = ScanNumber,
names_sep = "_",
values_from = c(Fat, Lean)
)
但是,我得到的输出不再包含StudyID和Timepoint。我需要这些变量才能将数据集与另一个数据集合并。我一直在搜索互联网,但似乎无法找到执行ivot_wider后如何在新数据集中保留StudyID和时间点。我想念什么?
谢谢。
答案 0 :(得分:2)
在id_cols
中合并它们,将其保留(并分组):
dataset %>%
pivot_wider(
id_cols = c(Label, StudyID, Timepoint),
names_from = ScanNumber,
names_sep = "_",
values_from = c(Fat, Lean)
)
# # A tibble: 6 x 9
# Label StudyID Timepoint Fat_1 Fat_2 Fat_3 Lean_1 Lean_2 Lean_3
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 1.1 1 1 3 3 3 5 5 5
# 2 2.1 2 1 4 4 4 6 6 6
# 3 3.1 3 1 5 5 5 7 7 7
# 4 1.6 1 6 6 6 6 3 3 3
# 5 2.6 2 6 7 7 7 4 4 4
# 6 3.6 3 6 8 8 8 5 5 5