Recode Numeric Values in R

Recode numeric values in R

This function does not work on numeric vector. If you want to use it, you can do as follows:

 x <- 1:10 # your numeric vector

as.numeric(revalue(as.character(x), c("2" = "33", "4" = "88")))

# [1] 1 33 3 88 5 6 7 8 9 10

How to recode a range of values in a variable and coerce it to a factor?

Using cut with labels:

df$education_recoded <- cut(df$education, breaks = c(0,3,5), 
labels = c("Up to secondary school",
"University studies or higher"))
# compare the values
table(df$education_recoded, df$education)
# 1 2 3 4 5
# Up to secondary school 21 24 24 0 0
# University studies or higher 0 0 0 15 16

Or using pipes:

library(dplyr)  

df %>%
mutate(education_recoded = cut(education, breaks = c(0,3,5),
labels = c("Up to secondary school",
"University studies or higher")))

Recode Numeric To Character

The 'v1' can be used as an index as it is a sequence of values from 1. So, if we pass the new vector to rename in the order we want, then 'pass' will replace whereever there is 1 value in 'v1', 'arb' for 2 and 'rtk' for 3

library(data.table)
setDT(have)[, v1 := c('pass', 'arb', 'rtk')[v1]]

Another option is using a named vector to do the match

nm1 <- setNames(c('pass', 'arb', 'rtk'),  1:3)
setDT(have)[, v1 := nm1[as.character(v1)]]

Or do this with a factor label option

setDT(have)[, v1 := as.character(factor(v1, levels = 1:3, 
labels = c('pass', 'arb', 'rtk')))]

In the OP's code, the recode is using comparison operator (==), instead it would be =. Also, for values that are numeric, wrap with backquote

setDT(have)[, v1  := dplyr::recode(v1, `1` = "pass", `2`="arb", `3`="rtk")]

How do I recode multiple variables from string to numeric?

The following method seems to have worked for my issue (recoding string variables to numeric in multiple columns):

For_Analysis <- data.frame(Q11_1=c("Never", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))

New_Values <- c(1, 2, 3, 4, 5)
Old_Values <- unique(For_Analysis$Q11_1)

For_Analysis[1:3] <- as.data.frame(sapply(For_Analysis[1:3],
mapvalues, from = Old_Values, to = New_Values))

Thanks for the help!

Recode a range of values into one number using 'recode()' in tidyverse

It may be easier with case_when

library(dplyr)
case_when(var %in% 10:30 ~ 4,
var %in% 6:9 ~ 3,
var %in% 3:5 ~ 2,
var %in% 1:2 ~ 1,
var == 0 ~ 0)

Or another option is cut

as.integer(cut(var, breaks = c(-Inf, 0, 2, 5, 9, 30, Inf)))

NOTE: change the include.lowest and right option in cut to adjust

data

set.seed(24)
var <- sample(0:35, 50, replace = TRUE)

Recode character IDs into numeric IDs

library(dplyr)
library(stringr)
df %>%
mutate(
group = str_extract(id, "[0-9]+")
) %>%
group_by(group) %>%
mutate(id = as.numeric(paste0(group, if(n() > 1) row_number() else ""))) %>%
ungroup() %>%
select(-group)
# # A tibble: 10 × 2
# id Grade
# <dbl> <dbl>
# 1 11 3
# 2 21 3
# 3 22 3
# 4 331 4
# 5 332 4
# 6 333 4
# 7 351 5
# 8 352 5
# 9 353 5
#10 354 5


Related Topics



Leave a reply



Submit