Jack Jack - 1 month ago 7
R Question

How to recode dataset based on the values?

I have a big dataset with its format being similar to the followings:

names <- c('s1','s2','s3', 's4', 's5','s6', 's7', 's8','s9')
metals <- c(4.2, 5.3, 5.4,6, 7,8.5,0, 10.1,11)
plastics <- c(5.1, 0, 2.4,6.1, 7.7,5.5,1.99, 0 ,2.5)
grade<- c("AA", "AB", "AB", "AB", "AC" , "AB", NA , NA, NA)
my_df <- data.frame(names, metals, plastics, grade )


I need to recode each column For numeric columns I need to assign 1 where the value is greater than 0 and for the "grade" columns lets assume I want AA=1, AB=2, AC=3. what is the most efficient way to do so?

Thanks!

MFR MFR
Answer

Not sure if this one is the most efficient one, but we can use recode in car package for the character column.

my_df$metals <- ifelse (my_df$metals > 0, 1 , 0)

my_df$plastics <- ifelse (my_df$plastics > 0, 1 , 0)

library(car)
my_df$grade<-recode(my_df$grade, "'AA'=1; 'AB'='2'; 'AC'='3'")

Output

names metals plastics grade
1    s1      1        1     1
2    s2      1        0     2
3    s3      1        1     2
4    s4      1        1     2
5    s5      1        1     3
6    s6      1        1     2
7    s7      0        1  <NA>
8    s8      1        0  <NA>
9    s9      1        1  <NA>
Comments