一个印度的R包? [英] An R package for India?

查看:130
本文介绍了一个印度的R包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在美国做了很多R县的统计分析。但我也想为印度做一些研究。我找到了状态图,但在R中没有分区图。我可以在d3.js中找到这样的事情,但我宁愿不放弃R。



是否有R包对于印度来说,与'maps'类似。

.org /rel =nofollow noreferrer> GADM ,其中包含不同级别的行政区划的shapefile,所以地区级别也是我认为的级别2。
您可以使用下面的脚本直接加载数据,代码取自数据并使用 maptools加载shapefile 。



获取数据的代码。

 #加载所需的库
库(sp)

#从GADM加载文件
#使用ISO3C $指定文件名的国家b $ b#像阿富汗的AFG 。
#level指定管理级别。
loadGADM <-function(fileName,level = 0,...){
load(url(paste(http://gadm.org/data/rda/,fileName,_adm ,level,。RData,sep =)))
gadm
}

#将前缀(ISO3C代码)添加到shapefile中。
changeGADMPrefix< -function(GADM,前缀){
GADM< - spChFIDs(GADM,paste(前缀,row.names(GADM),sep =_))
GADM


#加载文件并更改前缀
loadChangePrefix< -function(fileName,level = 0,...){
theFile< - loadGADM(fileName, level)
theFile< - changeGADMPrefix(theFile,fileName)
theFile
}

#应用所有函数:
getCountries< - function fileName,level = 0,...){
多边形< - sapply(fileNames,loadChangePrefix,level)
polyMap< - do.call(rbind,polygon)
polyMap
}


I am doing lots of statistical analysis on county basis in R for US. But I want to do some studies for India as well. I have found state map, but no district map in R. I can find such things in d3.js but I would rather not abandon R.

Is there a R package for India that is similar to 'maps'.

解决方案

You can use data from GADM which contains shapefiles on different levels of administrative division so also district level which is level 2 I guess. You can use the script below to directly load the data, code is taken from here.

So in your case you would run:

IND<-getCountries("IND",level=2)

Just to check, plot the data:

plot(ind)

Alternatively you can use GAUL data and load the shapefile using maptools.

Code to get data.

# Load required libraries
library(sp)

# Load file from GADM
# Specify the countries for fileName using ISO3C
# like "AFG" for Afghanistan.
# "level" specifies adminsitrative level.
loadGADM<-function(fileName,level=0,...){
load(url(paste("http://gadm.org/data/rda/",fileName,"_adm",level,".RData",sep = "")))
gadm
}

# Add prefix (ISO3C code) to shapefile.
changeGADMPrefix<-function(GADM, prefix) {
GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
GADM
}

# Load file and change prefix
loadChangePrefix<-function (fileName, level = 0, ...) {
theFile <- loadGADM(fileName, level)
theFile <- changeGADMPrefix(theFile, fileName)
theFile
}

# Apply all the functions:
getCountries <- function (fileNames, level = 0, ...) {
polygon <- sapply(fileNames, loadChangePrefix, level)
polyMap <- do.call("rbind", polygon)
polyMap
}

这篇关于一个印度的R包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆