Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is recode in R not changing the original values?

Tags:

r

statistics

I'm trying to use recode in R (from the car package) and it is not working. I read in data from a .csv file into a data frame called results. Then, I replace the values in the column Built_year, according to the following logic.

recode(results$Built_year, 
       "2 ='1950s';3='1960s';4='1970s';5='1980s';6='1990s';7='2000 or later'")

When I check results$Built_year after doing this step, it appears to have worked. However, it does not store this value, and returns to its previous value. I don't understand why.

Thanks.

(at the moment something is going wrong and I can't see any of the icons for formatting)

like image 400
djq Avatar asked May 29 '10 16:05

djq


People also ask

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 recode a character value 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.

How do I change a variable value in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.


2 Answers

You need to assign to a new variable.

Taking the example from recode in the car package

R> x <- rep(1:3, 3)
R> x
[1] 1 2 3 1 2 3 1 2 3
R> newx <- recode(x, "c(1,2)='A'; else='B'")
R> newx
[1] "A" "A" "B" "A" "A" "B" "A" "A" "B"
R> 

By the way, the package is called car, not cars.

like image 121
Dirk Eddelbuettel Avatar answered Oct 20 '22 22:10

Dirk Eddelbuettel


car::recode (and R itself) is not working as SPSS Recode function, so if you apply transformation on a variable, you must assign it to a variable, as Dirk said. I don't use car::recode, although it's quite straightforward... learn how to deal with factors... as I can see, you can apply as.numeric(results$Built_year) and get same effect. IMHO, using car::recode in this manor is trivial. You only want to change factor to numeric, right... Well, you'll be surprised when you see that:

> x <- factor(letters[1:10])
> x
 [1] a b c d e f g h i j
Levels: a b c d e f g h i j
> mode(x)
 [1] "numeric"
> as.numeric(x)
 [1]  1  2  3  4  5  6  7  8  9 10

And, boy, do I like answering questions that refer to factors... =) Get familiar with factors, and you'll see the magic of "recode" in R! =) Rob Kabacoff's site is a good starting point.

like image 25
aL3xa Avatar answered Oct 20 '22 20:10

aL3xa