본문 바로가기
R

(R) Ways to change the name of element in a variable / revalue()/ ifelse()

by jangpiano 2020. 9. 1.
반응형

<Change the name of elements of a variable 

>library(MASS)

> View(birthwt)



> ?birthwt



> table(birthwt$race)


 1  2  3 

96 26 67 


> table(birthwt$smoke)


  0   1 

115  74


< using Ifelse() in dplyr>


The variable 'smoke' express 'smoking status during pregnancy' containing elements 0,1.

Actually we cannot really know which elements express 'No smoke' or 'smoke' because they are just numbers. 

For readers of the graph, I will assign 'NO smoke' to 0 and 'smoke' to 1 so that the readers can understand the graph at once. 


> table(birthwt$smoke)


  0   1 

115  74


> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(smoke))


> birthwt$smoke<-ifelse(birthwt$smoke==0,"NO smoke","smoke")


> table(birthwt$smoke)


NO smoke    smoke 

     115       74 

> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(smoke))


< using revalue() in plyr>

The variable 'race' express 'mother's race' containing elements 1,2,3 which respectively means 'white','black','others'.

For the example above, we use ifelse() in dplyr. 

However for this case, It will be complicated using ifelse() because it has three elements. 


For using revalue() in plyr, firstly change the variable from integer type to factory type.


> str(birthwt$race)

 int [1:189] 2 3 1 1 1 3 1 3 1 1 ...


> birthwt$race<-factor(birthwt$race)

> str(birthwt$race)

 Factor w/ 3 levels "1","2","3": 2 3 1 1 1 3 1 3 1 1 ...


> levels(birthwt$race)

[1] "1" "2" "3"

> table(birthwt$race)

 1  2  3 

96 26 67 


> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(race))


> birthwt$race<-revalue(birthwt$race,c("1"="white","2"="black","3"="others"))


> levels(birthwt$race)

[1] "white"  "black"  "others"

> table(birthwt$race)

 white  black others 

    96     26     67 


> ggplot(data=birthwt,aes(x=bwt))+geom_histogram(fill="white",colour="black")+facet_grid(vars(race))

> ggplot(data=birthwt,aes(x=bwt,fill=race))+geom_histogram(position="identity")

> ggplot(data=birthwt,aes(x=bwt,fill=race))+geom_histogram(position="identity",alpha=0.5)

> ggplot(data=birthwt,aes(x=bwt,fill=smoke))+geom_histogram(position="identity")

> ggplot(data=birthwt,aes(x=bwt,fill=smoke))+geom_histogram(position="identity",alpha=0.5)


              < ifelse() in dplyr>                                          <revalue() in plyr>

>library(dplyr)

 > birthwt$smoke<-ifelse(birthwt$smoke==0,"NO smoke","smoke")

 >birthwt$smoke<-factor(birthwt$smoke)

 >library(plyr)

 >birthwt$smoke<-revalue(birthwt$smoke, c("0"="NO smoke","1"="smoke"))


 

>birthwt$race<-ifelse(birthwt$race==1,"white",ifelse(birthwt$race==2,"black", "others"))

 >birthwt$race<-factor(birthwt$race)

 > birthwt$race<-revalue(birthwt$race,c("1"="white","2"="black","3"="others"))




반응형