I'm so confused here. I have a dataset that looks like this:
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)
)
I want to pivot_wider so that I have triplicate Fat and Lean measurements for each StudyID and Timepoint. You can see the Label contains information on the StudyID and Timepoint combined (for example, say StudyID = 1 and Timepoint = 6, Label is 1.6). This is how I am doing it:
newdataset <- dataset %>%
pivot_wider(
id_cols = Label,
names_from = ScanNumber,
names_sep = "_",
values_from = c(Fat, Lean)
)
However, the output I get no longer includes StudyID and Timepoint. I require these variables to then merge the dataset with another dataset. I have been searching the internet but can't seem to find how to keep StudyID and Timepoint in the new dataset after performing pivot_wider. What am I missing?
Thanks in advance.