假设我的数据框看起来像
category longitude latitude a -83.7 26.2 b -83.7 26.2 b -84.6 26.2 c -82.4 25.7 a -80.1 27.7 b -75.9 34.5
根据每个单元格中出现的唯一类别数量创建栅格地图的最佳方法是什么?
对地理空间数据不是很熟悉,我把它放在我一直使用的方法之下。但是,可能会有更好/更快的解决方案。
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.plugins.git.GitSCM" plugin="git@3.0.0">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>git@gitlab.mkmeier.com:msn/gitgud.git</url>
<credentialsId>GitLab_SSH</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders/>
<publishers/>
<buildWrappers/>
</project>
答案 0 :(得分:1)
这是一种即使对大数据也应该快速的方法。
我会在这里使用数据表符号,但如果你愿意的话,你可以用基数R或dplyr表示法做到这一点。
library(data.table)
dt = data.table(foo)
r = raster(vals=0, xmn=-85, xmx=-75, ymn=25, ymx=35, res=1)
# add a new column to dt specifying which raster cell each row is in
dt[, rastercell := cellFromXY(r, .SD[,.(longitude, latitude)])]
# aggregate how many unique categories there are in each raster cell
dt.tab <- dt[, length(unique(category)), by = rastercell]
# update the values of the raster with these values
r[dt.tab[, rastercell]] <- dt.tab[, V1]
# and plot
plot(r)
基础R表示法中的相同内容,如果您愿意:
r = raster(vals=0, xmn=-85, xmx=-75, ymn=25, ymx=35, res=1)
foo$rastercell <- cellFromXY(r, foo[,2:3])
foo.tab <- aggregate(category~rastercell, data=foo, FUN=function(x) length(unique(x)))
r[foo.tab[, "rastercell"]] <- foo.tab[, "category"]
plot(r)