i saw some similar posts but couldn't work out mine.. T_T
basically i have a data like below from column rate1 to ind.. and i wanted to create another column "outcome" to extract the value from column rate1 to rate3 depending on column ind..
i could do it with case_when but when there are 20 rates.. i'm thinking there must be a way to "dynamically call" the column value??
please help?? thanks~!!!
sample photo:
sample code:
library(tidyverse)
df <- data.frame(stringsAsFactors = FALSE,
rate1 = c(8, 6, 1, 10, 10),
rate2 = c(6, 2, 2, 4, 3),
rate3 = c(10, 7, 4, 8, 4),
ind = c(1, 3, 1, 2, 3)
)
df <- df %>%
mutate(outcome = case_when(ind == 1 ~ rate1,
ind == 2 ~ rate2,
ind == 3 ~ rate3))