data <- data.frame(id=c(1,2,3,4,5,6,7),
q1=c(3,4,5,2,1,2,4),
q2=c(3,4,4,5,4,3,2),
q3=c(2,3,2,3,1,2,3),
q4=c(3,4,4,4,4,5,5))
For these q1-q4 I would like to write a statement where it says if q1 = 1 then generate a q1_1 =1 ; if q1=2 then generate q1_2 = 2;if q1=3 then generate q1_3=3; if q1=4 then generate q1_4=4, and if q5=5 then generate q1_5=5 for all of the questions in this dataset. I know I would have to do some sort of loop and then maybe an if statement, but I am just not really familiar with loops at all.
The OUTPUT i am hoping to get looks like (but with more columns for all the questions)
id q1 q2 q3 q4 q1_1 q1_2 q1_3 q1_4 q1_5
1 1 3 3 2 3 NA NA 3 NA NA
2 2 4 4 3 4 NA NA NA 4 NA
3 3 5 4 2 4 NA NA NA NA 5
4 4 2 5 3 4 NA 2 NA NA NA
5 5 1 4 1 4 1 NA NA NA NA
6 6 2 3 2 5 NA 2 NA NA NA
7 7 4 2 3 5 4 NA NA 4 NA
Any help is appreciated, thank you!
CodePudding user response:
No loop necessary. Go long, then go wide, then join the original data.
library(tidyverse)
data <- tibble(id=c(1,2,3,4,5,6,7),
q1=c(3,4,5,2,1,2,4),
q2=c(3,4,4,5,4,3,2),
q3=c(2,3,2,3,1,2,3),
q4=c(3,4,4,4,4,5,5))
data |>
pivot_longer(-id) |>
mutate(name = paste(name, value, sep = "_")) |>
pivot_wider() |>
(\(d) left_join(data, d, by = "id"))()
#> # A tibble: 7 x 20
#> id q1 q2 q3 q4 q1_3 q2_3 q3_2 q4_3 q1_4 q2_4 q3_3 q4_4
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 3 3 2 3 3 3 2 3 NA NA NA NA
#> 2 2 4 4 3 4 NA NA NA NA 4 4 3 4
#> 3 3 5 4 2 4 NA NA 2 NA NA 4 NA 4
#> 4 4 2 5 3 4 NA NA NA NA NA NA 3 4
#> 5 5 1 4 1 4 NA NA NA NA NA 4 NA 4
#> 6 6 2 3 2 5 NA 3 2 NA NA NA NA NA
#> 7 7 4 2 3 5 NA NA NA NA 4 NA 3 NA
#> # ... with 7 more variables: q1_5 <dbl>, q1_2 <dbl>, q2_5 <dbl>, q1_1 <dbl>,
#> # q3_1 <dbl>, q4_5 <dbl>, q2_2 <dbl>