Selecting a large range of values to recode in R
I want to recode a large number of variables across multiple columns. Here is an example df
df <- data.frame(
id_number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
age = c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
abc1 = c(501, 502, 503, 504, 505, 506, 507, 508, 509, 510),
abc2 = c(501, 502, 501, 501, 502, 501, 502, 503, 501, 502),
abc3 = c(501, 506, 501, 501, 510, 501, 510, 501, 501, 501),
abc4 = c(507, 505, 501, 501, 501, 501, 501, 501, 501, 501)
)
df
The columns abc1:abc4 has values 501:510 and I am trying to recode 501:508 as 91, 509 as 92 and 510 as 93 across all these columns at once. Here is what I tried -
library(dplyr)
df1 <-
df %>%
mutate(across(
abc1:abc4,
~ recode(
.x,
`501:508` = 91L,
`509` = 92L,
`510` = 93L
)
))
But I get an error
x NAs introduced by coercion ℹ Input ..1
is across(abc1:abc4, ~recode(.x, `501:508` = 91L, `509` = 92L, `510` = 93L))
.NAs introduced by coercionProblem with mutate()
input ..1
. x Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default
However, it works if I change the values one by one but I want to do it all at once as my real data has a very long list of values. Am I doing something wrong with the part
`501:508` = 91L,
Thank you !
Follow-up question from above
Let's say the value of abc1:abc4 is way larger and there is an additional set of value of range 1-175. abc1:abc4 = c(1:175, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510). I just tweaked the values from my previous example to illustrate this here.
df1 <- data.frame(
id_number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
age = c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
abc1 = c(14, 158, 170, 504, 505, 506, 507, 508, 509, 510),
abc2 = c(501, 502, 501, 501, 45, 501, 502, 59, 501, 100),
abc3 = c(89, 506, 12, 501, 510, 13, 510, 501, 11, 501),
abc4 = c(32, 505, 35, 501, 501, 56, 501, 12, 501, 501)
)
df1
Now I want to recode them all together where 1:175 = 90, 501:508 = 91, 509 = 92 and 510 - 93 across columns abc1:abc4 at one go. How would "nm1" in @akrun's answer be set here. and is there an easier way to do this? Thank you !
from Recent Questions - Stack Overflow https://ift.tt/3gLe6U2
https://ift.tt/eA8V8J
Comments
Post a Comment