Here I give some tricks how to substitute categorical values with another ones.
Lets there is column denoted as "breed" and containing the following variables: "LW", "Y-LW", "D", "SM-1", "D-LW" and "L". Our aim is to change the values according to its abbreviation:
LW -- LargeWhite
Y-LW -- Yorkshire-LargeWhite
D -- Duroc
SM-1 -- EarlyRipeMeat
D-LW -- Duroc-LargeWhite
L -- Landrace
The good choice is to use "ifelse" function to get the point established. It is assumed, data frame "a" with second column "breed" is available. The script in R is as follows:
x<-ifelse (a2[2]=="LW","LargeWhite",
{ifelse (a2[2]=="Y-LW","Yorkshire-LargeWhite",
{ifelse (a2[2]=="D","Duroc",
{ifelse (a2[2]=="SM-1","EarlyRipeMeat",
{ifelse (a2[2]=="D-LW","Duroc-LargeWhite","Landrace")})})})})
a[2]<-as.factor(x) #transferring the variables from vector x to the second column of dataset
Lets there is column denoted as "breed" and containing the following variables: "LW", "Y-LW", "D", "SM-1", "D-LW" and "L". Our aim is to change the values according to its abbreviation:
LW -- LargeWhite
Y-LW -- Yorkshire-LargeWhite
D -- Duroc
SM-1 -- EarlyRipeMeat
D-LW -- Duroc-LargeWhite
L -- Landrace
The good choice is to use "ifelse" function to get the point established. It is assumed, data frame "a" with second column "breed" is available. The script in R is as follows:
x<-ifelse (a2[2]=="LW","LargeWhite",
{ifelse (a2[2]=="Y-LW","Yorkshire-LargeWhite",
{ifelse (a2[2]=="D","Duroc",
{ifelse (a2[2]=="SM-1","EarlyRipeMeat",
{ifelse (a2[2]=="D-LW","Duroc-LargeWhite","Landrace")})})})})
a[2]<-as.factor(x) #transferring the variables from vector x to the second column of dataset
No comments:
Post a Comment