This document demonstrates how to recode values in R by replacing values that meet certain conditions with other values or text. Various recoding techniques are shown including replacing values that are less than, equal to, or greater than a given value. Recoding is also demonstrated for categorical variables and replacing missing values.
This document demonstrates how to recode values in R by replacing values that meet certain conditions with other values or text. Various recoding techniques are shown including replacing values that are less than, equal to, or greater than a given value. Recoding is also demonstrated for categorical variables and replacing missing values.
This document demonstrates how to recode values in R by replacing values that meet certain conditions with other values or text. Various recoding techniques are shown including replacing values that are less than, equal to, or greater than a given value. Recoding is also demonstrated for categorical variables and replacing missing values.
This document demonstrates how to recode values in R by replacing values that meet certain conditions with other values or text. Various recoding techniques are shown including replacing values that are less than, equal to, or greater than a given value. Recoding is also demonstrated for categorical variables and replacing missing values.
x=c(1,4,5,6,8,9) > #Recoding the values less than 6 with zero > x[x<6]=0 >x [1] 0 0 0 6 8 9 > # Recoding the value 6 with NA > x[x==6]=NA >x [1] 0 0 0 NA 8 9 > A <-c(3, 2, NA, 5, 3, 7, NA, NA, 5, 2, 6) > is.na(A) [1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE > A[ is.na(A) ]=0 > #Recoding Categorical variable > gender <-c("MALE","FEMALE","FEMALE "," MALE","FEMALE") > gender[gender=="MALE"]=1 > gender [1] "1" "FEMALE" "FEMALE " " MALE" "FEMALE" > > #Recoding MALE by 1 and FEMALE by 2 using ifelse() > gender=c("MALE","FEMALE","FEMALE","MALE","MALE") > ifelse (gender == "MALE", 1,2) [1] 1 2 2 1 1 > > # Recoding MALE by 1, FEMALE by 2 and UNKNOWN by 3 using ifelse() > gender=c("MALE","FEMALE","FEMALE","UNKNOWN","MALE") > ifelse (gender == "MALE", 1, ifelse (gender == "FEMALE", 2,3)) [1] 1 2 2 3 1 > #Dat Frame > X=data.frame(Gender = c("F", "M", "M", "F", "B", "M", "M"), Height = c(154, 160, 130, 145, 169, 183, 166)) > #Recoding MALE by 1, FEMALE by 2 and other by 99 using ifelse() > X[,1]=ifelse(X[,1] == "M", 1, ifelse(X[,1] == "F", 2, 99)) >X Gender Height 1 2 154 2 1 160 3 1 130 4 2 145 5 99 169 6 1 183 7 1 166 > # Replacing all the data in a field with a number > X$Gender=5 >X Gender Height 1 5 154 2 5 160 3 5 130 4 5 145 5 5 169 6 5 183 7 5 166 > # Replacing all the data in a field with text > X$Gender="Two" >X Gender Height 1 Two 154 2 Two 160 3 Two 130 4 Two 145 5 Two 169 6 Two 183 7 Two 166 > #Replacing all the data in a field with NA (missing data) > X$Gender="NA" >X Gender Height 1 NA 154 2 NA 160 3 NA 130 4 NA 145 5 NA 169 6 NA 183 7 NA 166 > # Replacing the data in a field based on equal to some value > A=data.frame(Gender = c("F", "F", "M", "F", "B", "M", "M"), Height = c(154, 167, 178, 145, 169, 183, 176)) > A$Height[A$Height==169]="ONE Sixty NINE" > A$Height[A$Height<=169]="lessthanONE Sixty NINE" > A$Height[A$Height>=169]="greater thanONE Sixty NINE" >A Gender Height 1 F greater thanONE Sixty NINE 2 F greater thanONE Sixty NINE 3 M greater thanONE Sixty NINE 4 F greater thanONE Sixty NINE 5 B greater thanONE Sixty NINE 6 M greater thanONE Sixty NINE 7 M greater thanONE Sixty NINE > > #replacing only missing data > A=data.frame(Gender = c("F", NA, "M", NA, "B", "M", "M"), Height = c(154, 167, 178, 145, 169, 183, 176)) > A$Gender[is.na(A$Gender)]="Missing Grade" >A Gender Height 1 F 154 2 Missing Grade 167 3 M 178 4 Missing Grade 145 5 B 169 6 M 183 7 M 176 > > #creating the new column with NA > A$Gender1=NA >A Gender Height Gender1 1 F 154 NA 2 Missing Grade 167 NA 3 M 178 NA 4 Missing Grade 145 NA 5 B 169 NA 6 M 183 NA 7 M 176 NA > > #copying the data from the existing column into the new one. > A$Gender1=A$Gender > A$Gender1[A$Gender=="M"]=4 > > >A Gender Height Gender1 1 F 154 F 2 Missing Grade 167 Missing Grade 3 M 178 4 4 Missing Grade 145 Missing Grade 5 B 169 B 6 M 183 4 7 M 176 4 >