At the moment, my dataset is in a wide format, I would like to change it to a different format I have a data frame:
df <- structure(list(Pat = c(88L, 89L, 90L, 91L, 96L, 105L), res1 = c(63L,
77L, 73L, 61L, 101L, 86L), res2 = c(173L, 187L, 179L, 171L, 174L,
184L), res3 = c(369L, 519L, 669L, 719L, 619L, 419L), res4 = c(37L,
32L, 34L, 35L, 30L, 33L), res5 = c(79L, 61L, 89L, 93L, 46L, 77L
), res6 = c(204L, 180L, 190L, 184L, 149L, 180L), res1.1 = c(495L,
113L, 109L, 97L, 137L, 122L), res2.1 = c(209L, 209L, 209L, 209L,
209L, 209L), res3.1 = c(405L, 555L, 705L, 755L, 655L, 455L),
res4.1 = c(73L, 68L, 70L, 71L, 66L, 69L), res5.1 = c(115L,
97L, 125L, 129L, 82L, 113L), res6.1 = c(240L, 216L, 226L,
220L, 185L, 216L), res1.2 = c(413L, 563L, 713L, 763L, 663L,
463L), res2.2 = c(81L, 76L, 78L, 79L, 74L, 77L), res3.2 = c(123L,
105L, 133L, 137L, 90L, 121L), res4.2 = c(248L, 224L, 234L,
228L, 193L, 224L), res5.2 = c(539L, 157L, 153L, 141L, 181L,
166L), res6.2 = c(253L, 253L, 253L, 253L, 253L, 253L)), class = "data.frame", row.names = c(NA,
-6L))
I would like to bring it to this form:
data <- structure(list(Pat = c(88L, 89L, 90L, 91L, 96L, 105L, 88L, 89L,
90L, 91L, 96L, 105L, 88L, 89L, 90L, 91L, 96L, 105L), time = c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L), res1 = c(63L, 77L, 73L, 61L, 101L, 86L, 495L, 113L, 109L,
97L, 137L, 122L, 413L, 563L, 713L, 763L, 663L, 463L), res2 = c(173L,
187L, 179L, 171L, 174L, 184L, 209L, 209L, 209L, 209L, 209L, 209L,
81L, 76L, 78L, 79L, 74L, 77L), res3 = c(369L, 519L, 669L, 719L,
619L, 419L, 405L, 555L, 705L, 755L, 655L, 455L, 123L, 105L, 133L,
137L, 90L, 121L), res4 = c(37L, 32L, 34L, 35L, 30L, 33L, 73L,
68L, 70L, 71L, 66L, 69L, 248L, 224L, 234L, 228L, 193L, 224L),
res5 = c(79L, 61L, 89L, 93L, 46L, 77L, 115L, 97L, 125L, 129L,
82L, 113L, 539L, 157L, 153L, 141L, 181L, 166L), res6 = c(204L,
180L, 190L, 184L, 149L, 180L, 240L, 216L, 226L, 220L, 185L,
216L, 253L, 253L, 253L, 253L, 253L, 253L)), class = "data.frame", row.names = c(NA,
-18L))
I tried to do this with:
library(dplyr)
library(tidyr)
z <- gather(data, Var, Score, -1) %>%
separate(Var, into = c("time", "Var2"), sep = 3) %>%
group_by(time = match(time, unique(time))) %>%
mutate(Question = row_number()) %>%
select(-Var2)
But it does not give out what I need