I would like to first recode a value in a battery of questions and then rescale the results from 0 to 1. I read through here to learn how to apply sequential functions but my results are not rescaled.
var1<-sample(seq(1,11,1), size=100, replace=T)
var2<-sample(seq(1,11,1), size=100, replace=T)
library(tidyverse)
library(scales)
df<-data.frame(var1, var2)
df %>%
mutate(
across(
starts_with("var"), ~{
scales::rescale(as.numeric(car::Recode(.x, "11=5"), to=c(0,1)))
.x
}
))
There are two issues with your code. First, you mis-placed on of your closing parentheses. Second you did not reassign the recoded and rescaled vector back to .x
but instead return the original vector.
To fix both issues do:
df %>%
mutate(
across(
starts_with("var"), ~ scales::rescale(as.numeric(car::Recode(.x, "11=5")), to = c(0, 1))
)
)
#> var1 var2
#> 1 0.7777778 0.4444444
#> 2 0.1111111 0.0000000
#> 3 0.7777778 0.2222222
#> 4 0.3333333 0.7777778
#> 5 0.4444444 0.1111111
#> 6 0.4444444 0.7777778
#> 7 0.5555556 0.0000000
#> 8 1.0000000 0.5555556
#> 9 0.6666667 0.8888889
#> 10 1.0000000 0.3333333
#> 11 1.0000000 0.0000000
#> 12 0.0000000 1.0000000
#> 13 1.0000000 0.6666667
#> 14 0.5555556 1.0000000
#> 15 0.3333333 0.4444444
#> 16 0.4444444 0.4444444
#> 17 0.4444444 0.0000000
#> 18 0.3333333 0.3333333
#> 19 0.2222222 0.0000000
#> 20 0.4444444 0.0000000
#> 21 0.6666667 0.4444444
#> 22 0.6666667 0.4444444
#> 23 0.3333333 0.7777778
#> 24 0.5555556 0.6666667
#> 25 1.0000000 0.1111111
#> 26 0.1111111 0.1111111
#> 27 0.7777778 0.3333333
#> 28 0.4444444 1.0000000
#> 29 0.4444444 1.0000000
#> 30 0.8888889 0.8888889
#> 31 0.8888889 0.1111111
#> 32 0.4444444 0.6666667
#> 33 0.4444444 0.3333333
#> 34 0.7777778 0.3333333
#> 35 0.3333333 0.8888889
#> 36 0.1111111 0.8888889
#> 37 0.2222222 0.2222222
#> 38 0.3333333 0.4444444
#> 39 0.4444444 0.0000000
#> 40 0.4444444 0.6666667
#> 41 0.2222222 0.1111111
#> 42 0.3333333 0.1111111
#> 43 0.3333333 1.0000000
#> 44 0.7777778 0.5555556
#> 45 0.6666667 0.3333333
#> 46 1.0000000 0.6666667
#> 47 0.8888889 0.4444444
#> 48 0.5555556 0.5555556
#> 49 0.3333333 0.7777778
#> 50 0.5555556 0.8888889
#> 51 0.4444444 0.4444444
#> 52 0.1111111 0.0000000
#> 53 0.1111111 0.0000000
#> 54 0.5555556 0.4444444
#> 55 1.0000000 0.1111111
#> 56 0.6666667 0.4444444
#> 57 0.8888889 0.6666667
#> 58 0.6666667 0.3333333
#> 59 0.3333333 0.4444444
#> 60 0.6666667 0.5555556
#> 61 0.6666667 0.1111111
#> 62 0.5555556 0.1111111
#> 63 0.8888889 0.2222222
#> 64 0.5555556 0.7777778
#> 65 0.3333333 1.0000000
#> 66 0.4444444 0.4444444
#> 67 0.7777778 0.2222222
#> 68 0.5555556 0.2222222
#> 69 0.2222222 0.6666667
#> 70 0.7777778 0.1111111
#> 71 0.2222222 1.0000000
#> 72 0.1111111 1.0000000
#> 73 1.0000000 0.1111111
#> 74 0.4444444 0.4444444
#> 75 0.8888889 0.4444444
#> 76 0.6666667 0.2222222
#> 77 0.4444444 0.2222222
#> 78 0.8888889 0.6666667
#> 79 0.6666667 0.7777778
#> 80 0.3333333 0.7777778
#> 81 1.0000000 0.7777778
#> 82 0.2222222 0.0000000
#> 83 0.4444444 0.6666667
#> 84 0.1111111 0.4444444
#> 85 0.4444444 0.4444444
#> 86 0.8888889 0.4444444
#> 87 0.7777778 0.4444444
#> 88 0.7777778 0.8888889
#> 89 0.6666667 0.3333333
#> 90 0.1111111 0.2222222
#> 91 0.0000000 1.0000000
#> 92 0.4444444 0.7777778
#> 93 0.0000000 0.6666667
#> 94 0.3333333 0.4444444
#> 95 1.0000000 0.4444444
#> 96 0.4444444 0.0000000
#> 97 0.6666667 1.0000000
#> 98 0.8888889 0.6666667
#> 99 0.4444444 0.6666667
#> 100 0.1111111 0.3333333