简体   繁体   中英

Is there and R function for recoding variables?

I'm making the leap from SPSS to R but having a few teething issues.

I'm trying to recode a variable but I'm getting error messages.

Here are some examples of the SPSS code I'm trying to translate:

RECODE income (1, 2 = 1) (3, 4 = 2) INTO income2.
EXECUTE.

* Recode to String.
STRING sex_values (A8).
RECODE sex (1 = 'Male') (2 = 'Female') INTO sex_values.
EXECUTE.

Please take a look at a function funnily enough named as recode.

library(dplyr)
char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE)
recode(char_vec, a = "Apple")

Assuming you have a numeric vector containing numbers from the set 1, 2, 3, 4 and you want to replace 1 and 2 with "male" and 3 and 4 with "female" then here are some alternatives.

1) factor This creates such a factor. as.character(income2) can be used if you want a character vector instead.

income <- c(2, 1, 4, 3, 1, 4, 2)
income2 <- factor(income, levels = 1:4, labels = c("male", "male", "female", "female"))
income2
## [1] male   male   female female male   female male  
## Levels: male female

2) subscripting This creates a character vector

income2 <- c("male", "male", "female", "female")[income]
income2
## [1] "male"   "male"   "female" "female" "male"   "female" "male"  

3) car::recode There is a recode function in the car package (and also a slightly different one in the dplyr package and likely others in other packages). This creates a character vector. Add the as.factor = TRUE argument if you want the result to be a factor.

# assumes car package installed
income2 <- car::recode(income, "1:2='male';3:4='female'")
income2
## [1] "male"   "male"   "female" "female" "male"   "female" "male"  

As in many things in R, there are numerous ways to do this.

The first is to use the ifelse function. This is a useful function for reclassifying vectors as it evaluates in a way that conditions are met before replacement so, more complex statments are supported.

( x <- sample(1:2,20,replace=TRUE) )
  ifelse(x == 1, "male", ifelse(x == 2, "female", NA))
  ifelse(x == 1, "male", "female")
  ifelse(x == (1 | 2),"male","female")

You can also directly replace an element in the vector

x[x == 1] <- "male"
x[x == 2] <- "female"
print(x)

There is also using a factor and recoding using levels

( x <- as.factor(sample(1:2,20,replace=TRUE)) )
levels(x)[1] <- "male"
levels(x)[2] <- "female"
print(x)

You may use the if_else() function to replace/recode your data in R for simplicity.

The if_else(condition, true_result, false_result) check if the condition is meet, it returns true_result, if not, returns the false_result instead.

For example x= 3 and if_else(x>2,"yes","no) will return the output:

"yes"


So, in your case, you may use the following:


if_else(sex==1,"Male","Female")
if_else(income==(1 | 2),1,2)

In this case you can just put your data's sex column and income column in the above functions to recode them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM