Hi,
Maybe the question is really basic but I don't know why my full code does not work.
I have this simple data file:
data.frame(stringsAsFactors=FALSE,
URN = c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"),
E1 = c(1, 2, 3, NA, NA, NA, NA, NA, NA),
S1 = c("ddd", "s", NA, "fr", "rf", "f", "aa", "v", "bg"),
A2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
D1 = c(-1, 10, 6, -1, 8, 9, 7, -1, 99)
)
Now, I want to multiply all numeric variables including any value between 5 and 10 by 10 (I want to ignore variables like E1 and S1 in my example). I am using this code but I don't know how to modify that to exclude numerical variables without values 5-10:
step1 <- source %>% mutate_at(vars(matches("[A-Z]\\d")), ~.x * 10)
Then I want to create 5 new variables. I use this code:
step2 <- step1 %>% mutate(
Dealer.Rec.Score = case_when(
A2 >= 10 & A2 <= 80 ~ 0,
A2 >= 90 & A2 <= 100 ~ 100),
A1.16 = case_when(
A2 >= 10 & A2 <= 60 ~ "1-6",
A2 >= 70 & A2 <= 80 ~ "7-8",
A2 >= 90 & A2 <= 100 ~ "9-10"),
D.16 = case_when(
D1 >= 10 & D1 <= 60 ~ "1-6",
D1 >= 70 & D1 <= 80 ~ "7-8",
D1 >= 90 & D1 <= 100 ~ "9-10"),
TDO = case_when(
D1 == 990 ~ "No",
D1 == -10 ~ "Not Required",
D1 >= 10 & D1 <= 100 ~ "Yes"),
TDY = case_when(
D1 == 990 ~ 0,
D1 >= 10 & D1 <= 100 ~ 100))
Maybe there is a way of running everything in one step (it did not work for me so I've decided to use two steps).
I am also hoping there is much clever way of recoding all variables including values from 1 to 10 to their "16" version instead of using this multiplied code for each variable I want to recode:
var.16 = case_when(
var1 >= 10 & var1 <= 60 ~ "1-6",
var1 >= 70 & var1 <= 80 ~ "7-8",
var1 >= 90 & D1 <= var ~ "9-10"),
Any thoughts?