Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- eur<-read_csv('GEOSTAT_grid_POP_1K_2011_V2_0_1.csv')
- #two ways to extract east and north
- eur$North <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,2]
- eur$East <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,3]
- eur<-eur%>%
- mutate(
- lat = as.numeric(gsub('.*N([0-9]+)[EW].*', '\\1', GRD_ID))/100,
- lng = as.numeric(gsub('.*[EW]([0-9]+)', '\\1', GRD_ID)) * ifelse(gsub('.*([EW]).*', '\\1', GRD_ID) == 'W', -1, 1) / 100
- )
- head(eur)
- #get each countries population total
- popu<-aggregate(eur$TOT_P, by=list(Category=eur$CNTR_CODE), FUN=sum)
- for (row in 1:nrow(popu)) {
- country <- popu[row, "Category"]
- TOT_P <- popu[row, "x"]
- }
- #now loop through each contry and find the North and east km with half the people either side
- for (row in 1:nrow(popu)) {
- country <- popu[row, "Category"]
- TOT_P <- popu[row, "x"]
- Coun<-filter(eur, DATA_SRC == country)
- #stop when you have half the population
- thresholdValue <- (TOT_P/2)
- #head east until you have counted half the people
- Coun<-Coun[order(Coun$East),]
- ix <- length(which(cumsum(Coun$TOT_P) <= thresholdValue))
- first<-Coun[ix,]$East
- #head south until you have counted half the people
- Coun<-Coun[order(Coun$North),]
- iy <- length(which(cumsum(Coun$TOT_P) <= thresholdValue))
- cat(country, first ,strtoi(Coun[iy,]$North))
- cat("\n")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement