1975-2011年的数据中。

1)分别统计每年人口最多的国家是哪个?有多少

2)统计出各个国家的1975-2011年的平均人口增长率

3)统计每年人口最多的十个国家

4)统计出每年人口最少的十个国家

5)结合洲的语言的分类,请得出如下结果

5.1)哪个洲的人口最多?哪个洲的人口最少?

每个洲的前3个国家人口排名

5.2)哪种语言的国家人口最多?

librery(xlsx)

data<-read.xlsx("urbanpop.xlsx",sheet_index=3)
i<-0

for(dt in data){
if(i==0){
i<-2
next}
else{
index<-which(dt == max(dt,na.rm=TRUE))
cat(as.character(data$country[index]),dt[index],"\n")

}

}

data$country[1]

(data$X2011[1]-data$X1975[1])^(1/(2011-1975))-1

paste(((data$X2011[1]-data$X1975[1])^(1/(2011-1975))-1)*100,"%",sep="")

for(i in 1:209){
cat(as.character(data$country[i]),"\t",paste(((data$X2011[i]-data$X1975[i])^(1/(2011-1975))-1)*100,"%",sep=""),"\n")

}

i<-0
year<-1975
for(dt in data){
if(i==0){
i<-2
next}
else{
countrys_id <- order(dt,decreasing=TRUE)[1:10]
cat(year,"\t")
for(index in countrys_id){
cat(as.character(data$country[index]),"\t")
}
year=year+1
cat("\n")

}

}

i<-0
year<-1975
for(dt in data){
if(i==0){
i<-2
next}
else{
countrys_id <- order(dt,decreasing=FALSE)[1:10]
cat(year,"\t")
for(index in countrys_id){
cat(as.character(data$country[index]),"\t")
}
year=year+1
cat("\n")

}

}

Asian<-c("Afghanistan", "Armenia", "Azerbaijan", "Bahrain", "Bhutan", "Cambodia", "Indonesia",
"Iran", "Iraq", "Israel", "Japan", "Kazakhstan", "Kuwait", "Malaysia", "Myanmar", "Nepal", "Oman",
"Pakistan", "Qatar", "Saudi Arabia", "Singapore", "Tajikistan", "Thailand", "Turkmenistan", "Uzbekistan", "Yemen",
"Bangladesh", "Georgia", "India", "Jordan", "North Korea", "South Korea", "Lao", "Lebanon", "Maldives", "Mongolia",
"Philippines", "Sri Lanka", "Timor-Leste", "Turkey", "United Arab Emirates","Brunei", "China", "Hong Kong, China",
"Kyrgyz Republic", "Macao, China", "Syria", "Vietnam")

Europe<-c("Albania", "Austria", "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia",
"Cyprus", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Latvia",
"Liechtenstein", "Lithuania", "Malta", "Netherlands", "Norway", "Portugal", "Russia", "Serbia", "Slovenia", "Sweden", "Ukraine",
"Andorra","Channel Islands", "Faeroe Islands", "Finland", "Iceland", "Ireland", "Isle of Man", "Italy", "Luxembourg", "Macedonia, FYR",
"Moldova", "Monaco", "Montenegro", "Poland", "Romania", "San Marino", "Slovak Republic", "Spain", "Switzerland", "United Kingdom")

Afrain<-c("Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi", "Chad", "Comoros",
"Cote d'Ivoire", "Djibouti", "Eritrea", "Ethiopia", "Guinea", "Kenya", "Lesotho", "Liberia", "Libya",
"Mauritania", "Mauritius", "Mozambique", "Namibia", "Niger", "Rwanda", "Sao Tome and Principe", "Seychelles",
"Sierra Leone", "Swaziland", "Tanzania", "Uganda", "Zambia", "Zimbabwe", "South Sudan","Cameroon",
"Central African Republic", "Egypt", "Equatorial Guinea", "Gabon", "Gambia", "Ghana", "Guinea-Bissau",
"Madagascar", "Malawi", "Mali", "Morocco", "Nigeria", "Senegal", "Somalia", "South Africa", "Sudan", "Togo","Tunisia",
"Cape Verde", "Congo, Dem. Rep.", "Congo, Rep.")

SouthAmerican<-c("Argentina", "Guyana", "Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela","Brazil", "Chile",
"Colombia", "Ecuador","Aruba","Belarus","Bolivia")

NorthAmerican<-c("Antigua and Barbuda", "Bahamas", "Barbados", "Canada", "Greenland", "Grenada",
"Guatemala", "Honduras", "Jamaica", "Nicaragua", "St. Kitts and Nevis", "Trinidad and Tobago","Belize",
"Bermuda", "Cayman Islands", "Costa Rica", "Cuba", "Dominica", "Dominican Republic", "El Salvador",
"Haiti", "Mexico", "Panama", "Puerto Rico", "St. Lucia", "St. Vincent and the Grenadines", "Turks and Caicos Islands",
"United States", "Virgin Islands (U.S.)")

Oceania<-c("Australia", "Kiribati", "New Caledonia", "New Zealand", "Palau", "Papua New Guinea", "Solomon Islands", "Tuvalu",
"American Samoa", "Fiji", "French Polynesia", "Guam", "Marshall Islands", "Northern Mariana Islands", "Samoa", "Tonga", "Vanuatu",
"Micronesia, Fed. Sts.")

AS_number<-0
AF_number<-0
EU_number<-0
SA_number<-0
NA_number<-0
OC_number<-0
other_number<-0
index<-1
for(country in data$country){
if(country %in% Asian){
AS_number= AS_number+data$X2011[index]
}else if(country %in% Europe){
EU_number = EU_number+data$X2011[index]
}else if(country %in% Afrain){
AF_number= AF_number+data$X2011[index]
}else if(country %in% SouthAmerican){
SA_number= SA_number+data$X2011[index]
}else if(country %in% NorthAmerican){
NA_number= NA_number+data$X2011[index]
}else if(country %in% Oceania){
OC_number= OC_number+data$X2011[index]
}else{
other_number= other_number +data$X2011[index]
}
index=index+1
}

cat("亚洲人口数","欧洲人口数","北美洲人口数","南美洲人口数","非洲人口数","大洋洲人口数","\n")
population<-c(AS_number,EU_number,NA_number,SA_number,AF_number,OC_number)
sort_pl<-order(population)
sort_pl

AS<-c()
AF<-c()
EU<-c()
SA<-c()
NAA<-c()
OC<-c()
AS_I<-c()
AF_I<-c()
EU_I<-c()
SA_I<-c()
NAA_I<-c()
OC_I<-c()
index<-1
dt_2011<-data$X2011
for(country in data$country){
if(country %in% Asian){
AS_I=c(AS_I,country)
AS=c(AS,dt_2011[index])
}else if(country %in% Europe){
EU_I=c(EU_I,country)
EU=c(EU,dt_2011[index])
}else if(country %in% Afrain){
AF_I=c(AF_I,country)
AF=c(AF,dt_2011[index])
}else if(country %in% SouthAmerican){
SA_I=c(SA_I,country)
SA=c(SA,dt_2011[index])
}else if(country %in% NorthAmerican){
NAA_I=c(NAA_I,country)
NAA=c(NAA,dt_2011[index])
}else if(country %in% Oceania){
OC_I=c(OC_I,country)
OC=c(OC,dt_2011[index])
}else{
print(country)
}
index=index+1
}
for(x in order(AS,decreasing=TRUE)[1:3]){
cat(AS_I[x],"\t","人口数",AS[x],"\n")
}
for(x in order(AF,decreasing=TRUE)[1:3]){
cat(AF_I[x],"\t","人口数",AF[x],"\n")
}
for(x in order(EU,decreasing=TRUE)[1:3]){
cat(EU_I[x],"\t","人口数",EU[x],"\n")
}
for(x in order(SA,decreasing=TRUE)[1:3]){
cat(SA_I[x],"\t","人口数",SA[x],"\n")
}
for(x in order(NAA,decreasing=TRUE)[1:3]){
cat(NAA_I[x],"\t","人口数",NAA[x],"\n")
}
for(x in order(OC,decreasing=TRUE)[1:3]){
cat(OC_I[x],"\t","人口数",OC[x],"\n")
}

没想到没有R语言的代码贴士。这里面最麻烦的是第五题,数据要自己去爬,去了百度百科还有个data.cn的网站,爬,但是还剩下50几个爬不出来,心里很难受。

说下注意的东西吧。1.是工作目录得注意,不然读取不到csv文件。

2.因为国家名称是以因子的形式读取出来的,因此得使用as.character()来转换一下。

感觉就这两点东西需要注意,这东西不难,但是第五题太繁琐。

R语言处理1975-2011年的人口信息的更多相关文章

  1. R语言中常用包(二)

    数据导入 以下R包主要用于数据导入和保存数据 feather:一种快速,轻量级的文件格式.在R和python上都可使用readr:实现表格数据的快速导入.中文介绍可参考这里readxl:读取Micro ...

  2. r语言 包说明

    [在实际工作中,每个数据科学项目各不相同,但基本都遵循一定的通用流程.具体如下]   [下面列出每个步骤最有用的一些R包] 1.数据导入以下R包主要用于数据导入和保存数据:feather:一种快速,轻 ...

  3. R语言进行数据预处理wranging

    R语言进行数据预处理wranging li_volleyball 2016年3月22日 data wrangling with R packages:tidyr dplyr Ground rules ...

  4. R语言实战(二)数据管理

    本文对应<R语言实战>第4章:基本数据管理:第5章:高级数据管理 创建新变量 #建议采用transform()函数 mydata <- transform(mydata, sumx ...

  5. R语言实现 广义加性模型 Generalized Additive Models(GAM) 入门

    转载请说明. R语言官网:http://www.r-project.org/ R语言软件下载:http://ftp.ctex.org/mirrors/CRAN/         注:下载时点击 ins ...

  6. R语言 推荐算法 recommenderlab包

    recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...

  7. R语言学习笔记:日期处理

    1.取出当前日期 Sys.Date() [1] "2014-10-29" date()  #注意:这种方法返回的是字符串类型 [1] "Wed Oct 29 20:36: ...

  8. R语言的前世今生(转)

    最近因病休养在家,另外也算是正式的离开Snack Studio.终于有了大把可以自由支配的时间.可以自主的安排.最近闲暇的时间总算是恶补了不少前段时间行业没有时间关注的新事物.看着行业里引领潮流的东西 ...

  9. R语言各种假设检验实例整理(常用)

    一.正态分布参数检验 例1. 某种原件的寿命X(以小时计)服从正态分布N(μ, σ)其中μ, σ2均未知.现测得16只元件的寿命如下: 159 280 101 212 224 379 179 264  ...

随机推荐

  1. URAL 2027 URCAPL, Episode 1 (模拟)

    题意:给你一个HxW的矩阵,每个点是一个指令,根据指令进行一系列操作. 题解:模拟 #include<cstdio> #include<algorithm> using nam ...

  2. vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象合并到总的对象,再提交

    vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象 ...

  3. Bootstrap历练实例:默认的列表组

    Bootstrap 列表组 本章我们将讲解列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-grou ...

  4. java,求1-100之和。

    package study01; public class TestWhile { public static void main(String[] args) { int sum = 0; int ...

  5. tomcat关闭钩子

    转 http://501565246-qq-com.iteye.com/blog/1733575 21,tomcat关闭钩子 博客分类: tomcat   在很多环境下,在关闭应用程序的时候需要做一些 ...

  6. HTTP-常用配置

    前言 这篇主要介绍HTTP服务程序环境 可能有一些介绍不到,博主能力有限,欢迎大神来纠正改进 HTTP协议从http/0.9到如今的http/2.0中间发生了很大的改变,现在主流的事http/1.1 ...

  7. vue渲染函数&JSX

    Vue推荐在绝大多数情况下使用template来创建你的HTML.然而在一些场景中,你真的需要JavaScript的完全编程能力,这时你可以使用render函数,它比template跟接近编译器. 虚 ...

  8. 如何用纯 CSS 创作一个方块旋转动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gjgyWm 可交互视频 ...

  9. 解决 viewer.js 动态更新图片导致无法预览的问题

    1.viewer.js 使用 Demo http://fengyuanchen.github.io/viewerjs/ 2.viewer.js 下载地址 https://github.com/feng ...

  10. 优酷项目之 ORM(数据库对象关系映射)代码重写

    前言: 我们在操作数据库时候一般都是通过sql代码来操作mysql数据库中相关数据,这就需要懂得sql语句,那么怎么样才能在不懂sql语句的情况下通过我们所学的python代码来实现对mysql数据库 ...