728x90
#1.
library(carData)
data(Chile)
Chile
for(i in 1:ncol(Chile)){
this.na<-is.na(Chile[,i])
cat(colnames(Chile)[i],'\t',sum(this.na),'\n')
}
#2.
nrow(Chile[!complete.cases(Chile),])/nrow(Chile)
#3.
tmp<-airquality
tmp$Ozone[is.na(tmp$Ozone)]<-0
tmp$Solar.R[is.na(tmp$Solar.R)]<-0
#4.
sort(airquality$Solar.R,decreasing = T)
#5.
tmp<-airquality[,c('Month','Day','Solar.R')]
tmp[order(tmp$Solar.R,decreasing = T),]
#6.
tmp<-airquality[,c('Month','Day','Solar.R')]
tmp[order(tmp$Solar.R,decreasing = T),][1:5,]
#7.
set.seed(1234)
idx<-sample(1:nrow(CES11),size=200,replace=F)
CES11.200<-CES11[idx,]
CES11.200
agg<-aggregate(CES11.200,by=list(CES11.200$urban),FUN=table)
agg
View(agg)
#8.
cnt<-round(nrow(CES11)/5)
idx<-sample(1:nrow(CES11),size=cnt,replace=F)
CES11.20<-CES11[idx,]
agg<-aggregate(CES11.20,by=list(CES11.20$education),FUN=table)
agg
#9.
menu<-c('김밥','라면','쫄면','칼국수','아메리카노')
combn(menu,3) #10가지
#10.
##10-1.
data("Leinhardt")
tmp<-Leinhardt[complete.cases(Leinhardt),]
agg<-aggregate(tmp$infant,by=list(tmp$region),FUN=mean)
agg
##10-2.
agg<-aggregate(tmp$infant,by=list(tmp$oil),FUN=mean)
agg
##10-3.
tmp_high<-tmp[which(tmp$income>mean(tmp$income)),]
tmp_low<-tmp[which(tmp$income<mean(tmp$income)),]
mean(tmp_high$infant)
mean(tmp_low$infant)
#11.
##11-1.
data("Ericksen")
agg<-aggregate(Ericksen$minority,by=list(Ericksen$city),FUN=mean)
agg
##11-2.
tmp<-Ericksen
tmp[which(Ericksen$minority>25),'minority']<-'high'
tmp[which(Ericksen$minority<25),'minority']<-'low'
agg1<-aggregate(tmp[,c('crime','poverty')],by=list(tmp$minority),FUN=mean)
agg1
##11-3.
tmp<-Ericksen
tmp[which(Ericksen$highschool>40),'highschool']<-'high'
tmp[which((Ericksen$highschool>28)&(Ericksen$highschool<40)),'highschool']<-'middle'
tmp[which(Ericksen$highschool<28),'highschool']<-'low'
agg1<-aggregate(tmp[,c('housing','crime','poverty')],by=list(tmp$highschool),FUN=mean)
agg1
##11-4.
#비례관계에 있다.
728x90
'기타 > R' 카테고리의 다른 글
[R]난생처음 R코딩&데이터 분석-12장 연습문제 (0) | 2021.12.02 |
---|---|
[R]KNN-Titanic (0) | 2021.11.26 |
[R]난생처음 R코딩&데이터 분석-11장 개념 (0) | 2021.11.22 |
[R]난생처음 R코딩&데이터 분석-10장 연습문제 (0) | 2021.11.22 |
[R]난생처음 R코딩&데이터 분석-9장 연습문제 (0) | 2021.11.21 |