【发布时间】:2021-07-04 04:17:36
【问题描述】:
lat <- c(45.08323,40.08323)
long <- c(-82.46797,-81.46797)
df <- data.frame(lat, long)
library(geonames) #To calibrate altitude
readLines(url("http://api.geonames.org/",open="r"))
options(geonamesUsername= "MyUsername") #Note you have to create a username one the
website AND enable webservices on your geonames user account at
https://www.geonames.org/manageaccount.
GNsrtm3(54.481084,-3.220625)
srtm3 lng lat
1 797 -3.220625 54.48108
GNsrtm3 一次只能接受两个值,但我想通过函数运行一个纬度和经度向量。我希望将所有三个值 strm3、lng 和 lat 存储在 data.frame df.results 中。我不擅长循环,但我尝试过
for(i in 1:length(df)){
df.result <- GNsrtm3(df$lat[i],df$long[i])
i = i + 1 }
df$alt <- df.result$srtm3
我只得到答案的第一行。所以它不接受向量。有什么见解吗?
【问题讨论】:
-
使用“nrow”代替“length”。数据帧的长度是列数,因为它是一个列表。并且不要增加循环计数器。它作为“for”函数的一部分自动发生。