The inevitable:
d <- tribble(
~obs, ~time, ~y,
1, "pre", 19,
2, "post", 18,
3, "pre", 17,
4, "post", 16,
5, "pre", 15,
6, "post", 14
)
d
y
, but three measured before some treatment and three measured after.y
-values for pre
and for post
.pivot_wider
.pre
values and three post
. Why did this happen?pivot_wider
needs to know which row to put each observation in.pivot_wider
, here obs
(only).obs
values, so 6 different rows.obs
2 and pre
, so that cell missing (NA
).obs
needs to say which subject provided which 2 observations.d2 <- tribble(
~subject, ~time, ~y,
1, "pre", 19,
1, "post", 18,
2, "pre", 17,
2, "post", 16,
3, "pre", 15,
3, "post", 14
)
d2
subject
shows which subject provided each pre
and post
.pivot_wider
, now only 3 rows, one per subject.subject
, and now a pre
and post
for each subject
.d2
.y
decreases over time, with subject 1 highest overall.pivot_wider
: nothing!unnest
:group_by
and summarize
to find stats by group.