Skip to content

Instantly share code, notes, and snippets.

@kkprakasa
Last active November 4, 2019 04:54
Show Gist options
  • Save kkprakasa/bd32cd530092e82aede913f7af879322 to your computer and use it in GitHub Desktop.
Save kkprakasa/bd32cd530092e82aede913f7af879322 to your computer and use it in GitHub Desktop.
# SAC
require(foreign)
require(reshape2)
require(stringi)
provinsi<-read.csv('https://raw.githubusercontent.com/pr4ka5a/Wilayah-Administratif-Indonesia/master/csv/provinces.csv', header=FALSE, stringsAsFactors=FALSE)
kabupaten<-read.csv('https://raw.githubusercontent.com/pr4ka5a/Wilayah-Administratif-Indonesia/master/csv/regencies.csv',header=FALSE, stringsAsFactors=FALSE)
names(provinsi) <- paste(c('R101','provinsi'))
provinsi$provinsi <- stri_trans_totitle(provinsi$provinsi)
names(kabupaten) <- paste(c('R102','R101','kabupaten'))
kabupaten$kabupaten <- stri_trans_totitle(kabupaten$kabupaten)
kabupaten[c(87,80,153,391,394),]$kabupaten <- paste(c('Kota Dumai', 'Kota Siak', 'Kota Batam', 'Kabupaten ToliToli', 'Kabupaten Tojo UnaUna' ))
kabupaten <- merge(provinsi, kabupaten, by='R101', all=TRUE)
c(R101,R407,R503,R507,R508A,R508B,R508C,R508D,R508E,FWT)
c(R1001C,R1002)
x<-read.dbf('hansos17_art_diseminasi.dbf')[,c(1,9,26,30:35,38)]
x0<-x[ x$R407 > 9,]
r503<-c(
"1"="Bekerja",
"2"="Sekolah",
"3"="Mengurus Ruta",
"4"="Lainnya"
)
hs<-merge(
provinsi,
dcast(aggregate(FWT ~ R101+R503+R507, x0, sum), R101 +R503 ~ R507, value.var='FWT'),
by='R101',
all.x=TRUE
)
hs$R503 <- r503[as.character(hs$R503)]
x1 <- x0[x0$R507 == 4,]
x1$R508A<-ifelse(!is.na(x1$R508_A),x1$FWT,NA)
x1$R508B<-ifelse(!is.na(x1$R508_B),x1$FWT,NA)
x1$R508C<-ifelse(!is.na(x1$R508_C),x1$FWT,NA)
x1$R508D<-ifelse(!is.na(x1$R508_D),x1$FWT,NA)
x1$R508E<-ifelse(!is.na(x1$R508_E),x1$FWT,NA)
# aggregate multiple column R508A-R508E
hs1<-
merge(
provinsi,
aggregate(x1[,c(11:15)],c(x1["R101"],x1["R503"]), sum, na.rm=T),
by='R101',
)
hs1$R503 <- r503[as.character(hs1$R503)]
write.csv(hs,paste0(format(Sys.time(), "%Y%m%d"),'-hansos-sac-01.csv'), row.names=F)
write.csv(hs1,paste0(format(Sys.time(), "%Y%m%d"),'-hansos-sac-02.csv'), row.names=F)
hs2 <- read.dbf('hansos17_rt_2_diseminasi.dbf')[,c(1,154,155,232)]
hs21<-dcast(aggregate(FWT ~ R101+R1001C,hs2,sum),R101 ~ R1001C,value.var='FWT')
hs22<-dcast(aggregate(FWT ~ R101+R1002,hs2,sum),R101 ~ R1002,value.var='FWT')
hs21<-merge(provinsi, hs21, by='R101', all.x=T)
hs22<-merge(provinsi, hs22, by='R101', all.x=T)
write.csv(hs21,paste0(format(Sys.time(), "%Y%m%d"),'-hansos-sac-03csv'), row.names=F)
write.csv(hs22,paste0(format(Sys.time(), "%Y%m%d"),'-hansos-sac-04.csv'), row.names=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment