Data Management

load("sejong_poll0328.rda")
str(sejong.poll.kr)
## 'data.frame':    44 obs. of  5 variables:
##  $ counts: int  21 194 259 393 443 117 1123 71 29 5 ...
##  $ vote  : Factor w/ 2 levels "찬성","반대": 1 2 1 2 1 2 1 2 1 2 ...
##  $ class : Factor w/ 5 levels "대신 등","3품이하현직",..: 1 1 2 2 3 3 5 5 4 4 ...
##  $ region: Factor w/ 10 levels "서울","유후사",..: 1 1 1 1 1 1 2 2 3 3 ...
##  $ color : chr  "cyan" "red" "cyan" "red" ...
sejong.poll.kr
##    counts vote       class region color
## 1      21 찬성     대신 등   서울  cyan
## 2     194 반대     대신 등   서울   red
## 3     259 찬성 3품이하현직   서울  cyan
## 4     393 반대 3품이하현직   서울   red
## 5     443 찬성 3품이하전직   서울  cyan
## 6     117 반대 3품이하전직   서울   red
## 7    1123 찬성    품관촌민 유후사  cyan
## 8      71 반대    품관촌민 유후사   red
## 9      29 찬성        수령   경기  cyan
## 10      5 반대        수령   경기   red
## 11  17076 찬성    품관촌민   경기  cyan
## 12    236 반대    품관촌민   경기   red
## 13      1 반대     대신 등   평안   red
## 14      6 찬성        수령   평안  cyan
## 15     35 반대        수령   평안   red
## 16   1326 찬성    품관촌민   평안  cyan
## 17  28474 반대    품관촌민   평안   red
## 18     17 찬성        수령   황해  cyan
## 19     17 반대        수령   황해   red
## 20   4454 찬성    품관촌민   황해  cyan
## 21  15601 반대    품관촌민   황해   red
## 22      2 반대     대신 등   충청   red
## 23     35 찬성        수령   충청  cyan
## 24     26 반대        수령   충청   red
## 25   6982 찬성    품관촌민   충청  cyan
## 26  14013 반대    품관촌민   충청   red
## 27      5 찬성        수령   강원  cyan
## 28     10 반대        수령   강원   red
## 29    939 찬성    품관촌민   강원  cyan
## 30   6888 반대    품관촌민   강원   red
## 31      1 반대     대신 등   함길   red
## 32      3 찬성        수령   함길  cyan
## 33     14 반대        수령   함길   red
## 34     75 찬성    품관촌민   함길  cyan
## 35   7387 반대    품관촌민   함길   red
## 36     55 찬성        수령   경상  cyan
## 37     16 반대        수령   경상   red
## 38  36262 찬성    품관촌민   경상  cyan
## 39    377 반대    품관촌민   경상   red
## 40      2 반대     대신 등   전라   red
## 41     42 찬성        수령   전라  cyan
## 42     12 반대        수령   전라   red
## 43  29505 찬성    품관촌민   전라  cyan
## 44    257 반대    품관촌민   전라   red
xtabs(counts~vote, data=sejong.poll.kr)
## vote
##  찬성  반대 
## 98657 74149
pie(xtabs(counts~vote, data=sejong.poll.kr), col=sejong.poll.kr$color)
title(main="전체 찬반")
text(x=0, y=c(0.4,-0.4), labels=c("98657", "74149"))

xtabs(counts~vote+class, data=sejong.poll.kr)
##       class
## vote   대신 등 3품이하현직 3품이하전직  수령 품관촌민
##   찬성      21         259         443   192    97742
##   반대     200         393         117   135    73304
sejong.poll.kr$class.2<-ifelse(sejong.poll.kr$class=="품관촌민", "품관촌민", "관료")
xtabs(counts~vote+class.2, data=sejong.poll.kr)
##       class.2
## vote    관료 품관촌민
##   찬성   915    97742
##   반대   845    73304
addmargins(xtabs(counts~vote+class.2, data=sejong.poll.kr))
##       class.2
## vote     관료 품관촌민    Sum
##   찬성    915    97742  98657
##   반대    845    73304  74149
##   Sum    1760   171046 172806
options(digits=3)
prop.table(xtabs(counts~vote+class.2, data=sejong.poll.kr), margin=2)
##       class.2
## vote    관료 품관촌민
##   찬성 0.520    0.571
##   반대 0.480    0.429
attach(sejong.poll.kr)
par(mfrow=c(1,2))
pie(xtabs(counts~vote+class.2, data=sejong.poll.kr[class.2=="관료",], drop=T), labels=c("찬성", "반대"), col=color)
title(main="관료들의 찬반")
text(x=0, y=c(0.4,-0.4), labels=c("915", "845"))
pie(xtabs(counts~vote+class.2, data=sejong.poll.kr[class.2=="품관촌민",], drop=T), labels=c("찬성", "반대"), col=color)
title(main="품관촌민의 찬반")
text(x=0, y=c(0.4,-0.4), labels=c("98657", "74149"))

par(mfrow=c(1,1))
xtabs(counts~vote+region, data=sejong.poll.kr[class.2=="관료",], drop=T)
##       region
## vote   서울 경기 평안 황해 충청 강원 함길 경상 전라
##   찬성  723   29    6   17   35    5    3   55   42
##   반대  704    5   36   17   28   10   15   16   14
xtabs(counts~vote+region, data=sejong.poll.kr[class.2=="품관촌민",], drop=T)
##       region
## vote   유후사  경기  평안  황해  충청  강원  함길  경상  전라
##   찬성   1123 17076  1326  4454  6982   939    75 36262 29505
##   반대     71   236 28474 15601 14013  6888  7387   377   257
xtabs(counts~vote+class, data=sejong.poll.kr[region=="서울",], drop=T)
##       class
## vote   대신 등 3품이하현직 3품이하전직
##   찬성      21         259         443
##   반대     194         393         117
barplot(xtabs(counts~vote+class, data=sejong.poll.kr[region=="서울",], drop=T), col=color)
title(main="서울의 찬반")
text(x=c(0.7, 1.9, 1.9, 3.1, 3.1), y=c(120, 450, 135, 500, 220), labels=c("194","393", "259", "117", "443"))
legend("topleft", inset=0.05, fill=c("cyan", "red"), legend=c("찬성", "반대"))

mosaicplot(xtabs(counts~class+vote, data=sejong.poll.kr[region=="서울",], drop=T), col=color, main="서울의 찬반", xlab="계급" , ylab ="찬반")

xtabs(counts~vote+region, data=sejong.poll.kr[class.2=="관료" & !region=="서울",], drop=T)
##       region
## vote   경기 평안 황해 충청 강원 함길 경상 전라
##   찬성   29    6   17   35    5    3   55   42
##   반대    5   36   17   28   10   15   16   14
barplot(xtabs(counts~vote+region, data=sejong.poll.kr[class.2=="관료" & !region=="서울",], drop=T), col=color)
title(main="지방 관료들의 찬반")
legend("topleft", inset=0.05, fill=c("cyan", "red"), legend=c("찬성", "반대"))

mosaicplot(xtabs(counts~region+vote, data=sejong.poll.kr[class.2=="관료" & !region=="서울",], drop=T), col=color, main="", xlab="계급", ylab="찬반")
title(main="지방 관료들의 찬반")

barplot(xtabs(counts~vote+region, data=sejong.poll.kr[class.2=="품관촌민",], drop=T), col=color)
title(main="품관촌민들의 지역별 찬반")
legend("topleft", inset=0.05, fill=c("cyan", "red"), legend=c("찬성", "반대"))

mosaicplot(xtabs(counts~region+vote, data=sejong.poll.kr[class.2=="품관촌민",], drop=T), col=color, main="품관촌민의 지역별 찬반", xlab="지역", ylab="찬반")

xtabs(counts~vote+class, data=sejong.poll.kr[region=="충청",], drop=T)
##       class
## vote   대신 등  수령 품관촌민
##   찬성       0    35     6982
##   반대       2    26    14013
prop.table(xtabs(counts~vote+class, data=sejong.poll.kr[region=="충청",], drop=T), margin=2)
##       class
## vote   대신 등  수령 품관촌민
##   찬성   0.000 0.574    0.333
##   반대   1.000 0.426    0.667
barplot(prop.table(xtabs(counts~vote+class, data=sejong.poll.kr[region=="충청",], drop=T), margin=2), col=color, ylim=c(0, 1.5), axes=F)
axis(side=2, at=c(0, 0.5, 1.0), labels=c("0", "50%", "100%"))
title(main="충청도의 계급별 찬반 비율")
legend("topleft", inset=0.05, fill=c("cyan", "red"), legend=c("찬성", "반대"))
text(x=c(0.7, 1.9, 1.9, 3.1, 3.1), y=c(0.5, 0.3, 0.8, 0.15, 0.65), labels=c(2, 35, 26, 6982, 14013))

mosaicplot(xtabs(counts~class+vote, data=sejong.poll.kr[region=="충청",], drop=T), col=color, main="", xlab="계급", ylab="찬반")
title(main="충청도의 찬반")

save.image(file="sejong_poll0328.rda")
savehistory(file="sejong_poll0328.Rhistory")
q("no")