Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recode numeric values in R

Tags:

r

numeric

recode

I want to recode some numeric values into different numeric values and have had a go using the following code:

survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5=3, 4=2,3=2,2=1,1=1))

I get the following error:

## Error: unexpected '=' in "survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5="

Where am I going wrong?

like image 510
Ash Avatar asked Jun 03 '14 09:06

Ash


People also ask

How do I recode a variable in numeric in R?

Firstly, we use recode() available in dplyr package (Wickham et al., 2020). Then, we use ifelse() function to recode the categorical data to numeric variables. Last, we learn match() function to rename the character variable to numeric one. The data type of all recoded variables can be converted to each other using as.

What does recode () do in R?

Recoding Variables in R Recoding allows you to create new variables and to replace existing values of a variables based on a criterion. This way we can replace the data for every row without any criteria.

How do I reclassify a variable in R?

Recoding a categorical variable The easiest way is to use revalue() or mapvalues() from the plyr package. This will code M as 1 and F as 2 , and put it in a new column.

How do I recode a level in R?

You can use recode() directly with factors; it will preserve the existing order of levels while changing the values. Alternatively, you can use recode_factor() , which will change the order of levels to match the order of replacements. See the forcats package for more tools for working with factors and their levels.


3 Answers

We can recode numeric values by using recode or case_when on dplyr 0.7.0.

library(dplyr)
packageVersion("dplyr")
# [1] ‘0.7.0’

x <- 1:10

# With recode function using backquotes as arguments
dplyr::recode(x, `2` = 20L, `4` = 40L)
# [1]  1 20  3 40  5  6  7  8  9 10

# Note: it is necessary to add "L" a numerical value.
dplyr::recode(x, `2` = 20, `4` = 40)
# [1] NA 20 NA 40 NA NA NA NA NA NA
# Warning message:
# Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default

# With recode function using characters as arguments
as.numeric(dplyr::recode(as.character(x), "2" = "20", "4" = "40"))
# [1]  1 20  3 40  5  6  7  8  9 10

# With case_when function
dplyr::case_when(
  x %in% 2 ~ 20,
  x %in% 4 ~ 40,
  TRUE ~ as.numeric(x)
)
#  [1]  1 20  3 40  5  6  7  8  9 10
like image 142
Keiku Avatar answered Oct 22 '22 18:10

Keiku


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
like image 39
Davide Passaretti Avatar answered Oct 22 '22 18:10

Davide Passaretti


Try this:

#sample data
set.seed(123); x <- sample(1:5, size = 10, replace = TRUE)

x
# [1] 2 4 3 5 5 1 3 5 3 3

#recode
x <- c(1, 1, 2, 2, 3)[ x ]

x
# [1] 1 2 2 3 3 1 2 3 2 2
like image 27
zx8754 Avatar answered Oct 22 '22 19:10

zx8754