我去了世界银行的数据库 - 并选择了2个文件 - GDP和识字率。直觉上我知道可能有一个相关性。因此,问题陈述是找出200多个(大约)国家60年来国内生产总值和识字率的相关性。从2个文件中获取某些数据到矩阵
这里是链接;
http://data.worldbank.org/indicator/NY.GDP.PCAP.CD?view=chart [国内生产总值]
http://data.worldbank.org/indicator/SE.ADT.LITR.ZS?view=chart [FOR LIT]
我.csv格式的数据,并跳过从上面几行后阅读。
然后,这是我开始写的代码;
Lit = read.csv("C:/DIRECTORY/API_SE.ADT.LITR.ZS_DS2_en_csv_v2.csv", skip = 3, header = TRUE, dec = ".")
Gdp = read.csv("C:/DIRECTORY/API_NY.GDP.MKTP.CD_DS2_en_csv_v2.csv", skip = 3, header = TRUE, dec = ".")
#creating a list of variables for each different year
#Without initializing the variables here, the code below did not work
for (i in 5:62)
{
assign(paste0("year", i), 0*i)
}
#running a loop for all the values of each dataset
#The desired result of this in 55 vectors (1 for each year) of some length
(as there are many missing values) which have in them values of gdp and lit
of the same country in the same row
for (y in 5:62){
for (c in 1:264){
#checking if values are available as many values are missing
q = is.na(Gdp[c,y])
r = is.na(Lit[c,y])
#now we will assign the values to the specific year
assign(paste0("year", y), c(Gdp[c,y], Lit[c,y]))
}}
我从中得到的是55个向量(标题year1到year55),每个向量有2个值。
我知道发生了什么是每个向量,只有最后一个共存值被设置(之前的那些被下一个等等替代,等等直到最后)。
现在,什么是理想的,是一种增长年份向量的方法,以便它包含给定年份的所有共存(即某个国家,某年某个国家同时具有gdp和光照值)值。
欢迎来到SO。我很难理解你的问题。你是在问如何将数据放在长时间的形式中,以便每个国家和每年的组合都有一行与gdp的观察结果对应,并点亮? – C8H10N4O2
嘿咖啡因 - 谢谢。我在询问如何将数据转换为矩阵形式,以便每个矩阵有一年的时间,并且有2列(GDP,LIT)和行数,因为有一个国家的GDP和LIT数据都是在那一年。 。 –
好的 - 如果你通过将代码包含到任何一个[可重现的例子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)下载并解压缩有问题的文件或(甚至更好的)小矢量示例版本(可能为year1-year3) – C8H10N4O2