Note, this differs from the question "How to reshape data from long to wide format?" in 2 ways:
- There are a multiplicity of destination columns to be qualified by the single factor column.
- The title of this question specified "tibble", implying
tidyverse
solutions are preferred.
Executing:
set.seed(14)
df <- data.frame(geo = LETTERS[1:2], time = c(2000,2000,2001,2001),
X = rnorm(4, 0, 1),
Y = rnorm(4, 0, 2))
head(df)
produces something like:
geo time X Y
1 A 2000 -0.6618498 -0.07228116
2 B 2000 1.7189542 2.46389035
3 A 2001 2.1216670 -0.12976154
4 B 2001 1.4971537 2.13798746
What I want to do is reshape to spread
time
and end up with two rows with values for geo
: A
and B
.
This would produce something like:
geo X.2000 Y.2000 X.2001 Y.2001
A -0.6618498 -0.07228116 2.1216670 -0.12976154
B 1.7189542 2.46389035 1.4971537 2.13798746