Home › Forums › R help (deprec) › Loops › Reply To: Loops
-
Hello! Here are some possible solutions.
library(tidyverse) library(tibble)Define the data
df īš¤- tribble( ~respondent, ~eval_period, ~formal_emp_ugx, ~personal_emp_ugx, ~casual_emp_ugx, "Aaron", "Baseline", 500, 600, 700, "Bob", "Endline", 600, 700, 800, "Charlie", "Midline", 700, 800, 900 )You can use the across function
There are two options. First you can use the
across()
function.
df %īšĨ% mutate(across(.cols = c("formal_emp_ugx", "personal_emp_ugx", "casual_emp_ugx"), .fns = ~ case_when(eval_period == "Baseline" ~ .x/3700, eval_period == "Endline" ~ .x/3800, eval_period == "Midline" ~ .x/3500)))## # A tibble: 3 à 5 ## respondent eval_period formal_emp_ugx personal_emp_ugx casual_emp_ugx ## īš¤chrīšĨ īš¤chrīšĨ īš¤dblīšĨ īš¤dblīšĨ īš¤dblīšĨ ## 1 Aaron Baseline 0.135 0.162 0.189 ## 2 Bob Endline 0.158 0.184 0.211 ## 3 Charlie Midline 0.2 0.229 0.257
The tilde,
~
tells R, I am about to give you an operation to apply on many columns. And the.x
signifier represents each of the columns listed (âformal_emp_ugxâ, âpersonal_emp_ugxâ, âcasual_emp_ugxâ).Once this is done, you can rename the variables.
Here are some tutorials on how to use the across function:
- Official documentation
-
Articla from Rebecca Barter
-
Video from IDG tech talk
Pivot longer
You could also first pivot the data to a longer format
df_long īš¤- df %īšĨ% pivot_longer(cols = c("formal_emp_ugx", "personal_emp_ugx", "casual_emp_ugx"), values_to = "ugx")Then it becomes easy to do what you need:
df_long %īšĨ% mutate(usd = case_when(eval_period == "Baseline" ~ ugx/3700, eval_period == "Endline" ~ ugx/3800, eval_period == "Midline" ~ ugx/3500))## # A tibble: 9 à 5 ## respondent eval_period name ugx usd ## īš¤chrīšĨ īš¤chrīšĨ īš¤chrīšĨ īš¤dblīšĨ īš¤dblīšĨ ## 1 Aaron Baseline formal_emp_ugx 500 0.135 ## 2 Aaron Baseline personal_emp_ugx 600 0.162 ## 3 Aaron Baseline casual_emp_ugx 700 0.189 ## 4 Bob Endline formal_emp_ugx 600 0.158 ## 5 Bob Endline personal_emp_ugx 700 0.184 ## 6 Bob Endline casual_emp_ugx 800 0.211 ## 7 Charlie Midline formal_emp_ugx 700 0.2 ## 8 Charlie Midline personal_emp_ugx 800 0.229 ## 9 Charlie Midline casual_emp_ugx 900 0.257
And you can pivot back at the end:
df %īšĨ% pivot_longer(cols = c("formal_emp_ugx", "personal_emp_ugx", "casual_emp_ugx"), values_to = "ugx") %īšĨ% mutate(usd = case_when(eval_period == "Baseline" ~ ugx/3700, eval_period == "Endline" ~ ugx/3800, eval_period == "Midline" ~ ugx/3500)) %īšĨ% pivot_wider(names_from = name, values_from = c(usd, ugx))## # A tibble: 3 à 8 ## respondent eval_period usd_formal_emâĻš usd_pâĻ² usd_câĻÂŗ ugx_fâĻâ´ ugx_pâĻâĩ ugx_câĻâļ ## īš¤chrīšĨ īš¤chrīšĨ īš¤dblīšĨ īš¤dblīšĨ īš¤dblīšĨ īš¤dblīšĨ īš¤dblīšĨ īš¤dblīšĨ ## 1 Aaron Baseline 0.135 0.162 0.189 500 600 700 ## 2 Bob Endline 0.158 0.184 0.211 600 700 800 ## 3 Charlie Midline 0.2 0.229 0.257 700 800 900 ## # âĻ with abbreviated variable names šâusd_formal_emp_ugx, ²âusd_personal_emp_ugx, ## # Âŗâusd_casual_emp_ugx, â´âugx_formal_emp_ugx, âĩâugx_personal_emp_ugx, ## # âļâugx_casual_emp_ugx