I am trying to reshape my dataset using dplyr gather and spread functions to move from this data shape:
library(tidyverse)
# test data set
df = tibble(factor = c("a","a","b","b"),
factor2 = c("d1","d2","d1","d2"),
value1 = round(rnorm(4),1)*10,
value2 = round(runif(4),2)*100)
that looks like this:
# A tibble: 4 x 4
factor factor2 value1 value2
<chr> <chr> <dbl> <dbl>
1 a d1 4 97
2 a d2 -21 10
4 b d1 -2 65
5 b d2 -14 93
to something that will look like this:
factor d1val1 d1val2 d2val1 d2val2
a 4 97 -21 10
b -2 65 -14 93
Ideally I would like to achieve this with dplyr spread / gather.