I have a dataset where I have self-report measure for students for different subscales (factor which contains some levels). I want to add new factor levels for each participant.
# A tibble: 12 x 3
first_name subscales value
<chr> <fct> <int>
1 P1 Emotion Regulation 5
2 P1 Empathy 7
3 P1 Family Support 10
4 P1 Gratitude 12
5 P1 Optimism 12
6 P1 Peer Support 9
7 P1 Persistence 5
8 P1 School Support 8
9 P1 Self-Awareness 7
10 P1 Self-Control 6
11 P1 Self-Efficacy 8
12 P1 Zest 12
#dput
structure(list(first_name = c("P1", "P1", "P1", "P1", "P1", "P1",
"P1", "P1", "P1", "P1", "P1", "P1"), subscales = structure(1:12, .Label = c("Emotion Regulation",
"Empathy", "Family Support", "Gratitude", "Optimism", "Peer Support",
"Persistence", "School Support", "Self-Awareness", "Self-Control",
"Self-Efficacy", "Zest"), class = "factor"), value = c(5L, 7L,
10L, 12L, 12L, 9L, 5L, 8L, 7L, 6L, 8L, 12L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -12L))
Let's say I want to add new factor levels for each participant such that:
Engaged Living
= Optimism
+ Zest
+ Gratitude
Emotional Competence
= Emotion Regulation
+ Self-Control
+ Empathy
,
My current workflow is to convert the df from long to wide and then back to long (pivot_longer
and pivot_wider
). This get's the job done but I'm wondering if there is another workflow that avoids doing this (i.e., keep the df in long format). I'm looking for a tidyverse/dplyr workflow.