我想将2d矩阵转换为具有额外列和函数计算值的数据框

时间:2019-10-01 00:35:24

标签: r

我有一个二维值矩阵。我想将这些添加到具有其他计算值的数据框中以描述观察结果。有大约2700万个值,我将其范围缩小到很大。

我一直在尝试for循环以遍历2d矩阵,测试想要观察的条件,并使用我自己的函数提供计算出的值。它不起作用,我真的看不出来为什么。
coords返回3个值向量,其中包括2d矩阵本身的值。计数只是为了快速调试,但即使一次也不会增加。

  count <- 0
  for(i in 1:cols) 
    {
    if(count > 25) break
    for(j in 1:rows) 
      {
      cell_info <- coords(i,j) #get the relevant info from the first cell to start off
      if(!is.na(cell_info[3]))
      {
        if(cell_info[1] < 50 && cell_info[1] > 49) 
          {
          count <- count
          if(cell_info[2] > -97.64 && cell_info[2] < -97)
            {
            x <- data.frame(Cell = paste(j, ",", i),
                          Latitude = cell_info[1],
                          Longitude = cell_info[2], 
                          Elevation = cell_info[3],
                          channels = "3,6,7,9,25"
                        )
            y <- rbind(y,x)
            count <- count + 1 #limit to 1000 for brevity
          }
        }
      }
    }
  }

我希望类似

小区纬度经度高程通道

35,55 49.5 -97.3 212 3,6,7,9,25

但是它什么也没得到

0 个答案:

没有答案