无法在R

时间:2016-12-17 07:03:24

标签: r excel latitude-longitude

我有一个地理区域的纬度和经度的方形网格。但是我只知道这个网格的4个角的纬度和经度值。使用这些我需要计算所有十字准线的纬度值。因此,我分别在 R 中为纬度和经度引发了一个嵌套循环程序。

   tllong<-67.481961

   sink("output_long.txt")
   for (i in c(1:11447)) {
     for (j in c(1:10335)) {
       tllong<- 67.481961 + (j-1)*0.0030769
       print(tllong)
     }

    }
    sink()

上述计划用于计算经度。 tllong 是网格左上角的经度值。 11447是纬度数,10335是经度数。

同样,我创建了一个计算纬度的程序。

    tllat<-36.348639

    sink("output_lat_again.txt")
    for (i in c(1:11447)) {
      for (j in c(1:10335)) {
        print(tllat)
      }
      tllat<- tllat - (i-1)*0.002508
    }
    sink() 

tllat 是左上角网格方块的纬度值。

因此,您可以看到循环首先计算第一行的所有lat,long值然后转到第二行,然后是第三行,依此类推。但是当我获得两个程序的导出文本文件时,我得到一个包含所有值的列。这对我来说没什么用处。我尝试使用 sink(“output_long.xlsx”) xlsx 格式导出R的输出结果但是当我得到excel文件时(经过4-5小时的常量)长时间循环)我无法打开它。错误消息显示文件已损坏或文件格式不同。我试过这3-4次但是徒劳无功。

那么如何在excel文件中导出这两个程序的结果,这样我就不会在单个列中获得所有值,而是以适当的矩阵形式(即lat的值,每个单元格中的long对应于lat的值,长在网格的相应十字线中)。

另外,如果有人可以告诉我如何将这两个程序一起运行以便我可以在同一个文件中的单次运行中获取lat-long值,那就太好了。

1 个答案:

答案 0 :(得分:0)

好像你想要创建10335 * 11447 = 118304745对lat / lon值。这是一个非常大的数字。那是对的吗?但是,我将展示应用于较小示例的过程。试试这个:

expect(wrapper.instance().checkBoxChecked()).equals(true);

最后,将创建#setting the values of parameters tllong<-67.481961 tllat<-36.348639 deltalong<-0.0030769 deltalat<-0.002508 #small example: you can set the following to the real values nlong<-10 nlat<-10 #create vectors of values without loops lat<-seq(tllat,by=deltalat,length.out=nlat) lon<-seq(tllong,by=deltalong,length.out=nlong) #now we build every possible pair of lat/lon values latlong<-expand.grid(lon=lon,lat=lat) #we export it to a csv file write.csv(latlong,"somefile.csv",row.names=FALSE,quote=FALSE) 。请记住,使用您的值,创建的文件将非常大。