Referencing this question:
How to reshape data from long to wide format
set.seed(45)
dat1 <- data.frame(
name = rep(c("firstName", "secondName"), each=4),
timeperiod = c("Q1","Q2","Q3","Q4"),
height = c(2,9,1,2,11,15,16,10),
weight=c(1,4,2,8,2,9,1,2)
)
dat1
name timeperiod height weight
1 firstName Q1 2 1
2 firstName Q2 9 4
3 firstName Q3 1 2
4 firstName Q4 2 8
5 secondName Q1 11 2
6 secondName Q2 15 9
7 secondName Q3 16 1
8 secondName Q4 10 2
Suppose I have the dataframe above with the generation code provided.
I want a dataset which is structured:
name Variable Q1 Q2 Q3 Q4
firstName height 2 9 1 2
firstName weight 1 4 2 8
secondName height 11 15 16 10
secondName weight 2 9 1 2
Looking for a solution using base R not tidyverse. Trying to do this with the reshape function but open to other base R functions.