for循环和填充导致R中的空矩阵

时间:2012-11-08 17:20:03

标签: r for-loop matrix

我正在尝试使用for循环计算结果来填充空矩阵。我不知道如何在for循环中指定索引,即,现在我的for循环代码不起作用,我无法将结果输入到我的矩阵中。

以下是我数据的一个子集:

> dput(eg)
structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("7", 
"8", "9", "10", "11"), class = "factor"), LONG = c(980.744064583783, 
983.838644208237, 978.459941419921, 984.841644157878, 968.200358699171, 
967.552217901586, 981.077638048121, 975.435563330951, 982.123246960536, 
975.383981047645, 978.181377755365, 975.001205420472, 980.410452250835, 
971.112535576725, 980.744064583783, 981.789215313314, 982.835292295612, 
975.435563330951, 982.457239633824, 976.095788761194, 977.468633236679, 
983.50423264086, 985.221850881562, 983.838644208237, 980.410452250835, 
970.785922305654, 983.838644208237, 981.789215313314, 975.435563330951, 
968.457651588967, 981.744668260738, 976.807567823793, 975.765695304413, 
977.850367309317, 967.487216377624, 967.487216377624, 981.121035207435, 
970.785922305654, 981.455144719747, 976.372538872237, 947.785924542022, 
949.961077537064, 953.277469897636, 956.588193000174, 954.107424886471, 
969.362874319781, 967.61505086351, 986.40950486552, 989.671406838413, 
914.2401235585), LAT = c(7497.04118692311, 7488.13522468594, 
7485.26645510239, 7482.63600422376, 7489.1136371139, 7492.78277930456, 
7495.20802780153, 7497.97375736711, 7493.50450717453, 7494.17837952228, 
7490.89595766574, 7492.21670554412, 7498.87433550932, 7493.41424827727, 
7497.04118692311, 7495.33760219375, 7493.63434928842, 7497.97375736711, 
7491.67140156528, 7494.30641015495, 7490.76739320858, 7489.96827686286, 
7484.59949728804, 7488.13522468594, 7498.87433550932, 7495.2484410159, 
7488.13522468594, 7495.33760219375, 7497.97375736711, 7483.48377437677, 
7491.5416779275, 7494.4346337625, 7496.14008888173, 7492.72950917327, 
7488.98777760417, 7488.98777760417, 7499.00376048698, 7495.2484410159, 
7497.17068662916, 7488.67706076623, 7444.0453546661, 7444.40653631031, 
7446.84930605798, 7449.2962767147, 7450.76636540362, 7494.995420297, 
7496.57764523632, 7465.87604399919, 7468.36476722201, 7434.9359208897
)), .Names = c("month", "LONG", "LAT"), row.names = c(740L, 741L, 
742L, 743L, 751L, 762L, 773L, 784L, 795L, 806L, 817L, 828L, 744L, 
745L, 746L, 747L, 748L, 749L, 750L, 752L, 753L, 754L, 755L, 756L, 
757L, 758L, 759L, 760L, 761L, 763L, 764L, 765L, 766L, 767L, 768L, 
769L, 770L, 771L, 772L, 774L, 775L, 776L, 777L, 778L, 779L, 780L, 
781L, 782L, 783L, 785L), class = "data.frame")

这是我到目前为止所做的:

# CREATE AN EMPRTY MATRIX FOR RESULTS
    nrow<-3 #there are 3 results calculated for each ncol
    ncol<-length(unique(eg$month))
    total<-matrix(0,nrow=nrow,ncol=ncol)

    month<-unique(eg$month)
    library(spatstat) #need spatstat for calculating the window & nndist
    W2<-ripras(eg$LONG,eg$LAT) #defining the window

    # FOR LOOP TO CALCULATE NNDIST < & > 1KM & == 0KM FOR EACH MONTHLY POINT PATTERN 

    for(i in month){
      m<-subset(eg, month==i) #subsetting my data for each month
      mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
      nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
      total[1,i]<-length(nnd[which(nnd>1)]) #Filling up the matrix with results 1
      total[2,i]<-length(nnd[which(nnd==0)]) 
      total[3,i]<-length(nnd[which(nnd<1)])
    }

我收到此错误消息:

Error in total[1, i] <- length(nnd[which(nnd > 1)]) : 
  no 'dimnames' attribute for array

编辑:

感谢@SvenHohenstein,我需要使用(i在seq_along(月份))或(i in 1:length(month)),但问题是我的月份是等级为“7”和“ 8" 。

我必须首先在运行循环之前重命名我的因子级别(现在可以正常工作!)。但是,如果没有重命名您的因素,还有另一种方法吗?

levels(eg$month)<-c(1:2) 
for(i in 1:length(month)){
  m<-subset(DF, month==i) #subsetting my data for each month
  mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
  nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
  total[1,i]<-length(nnd[nnd>1]) #Filling up the matrix with results 1
  total[2,i]<-length(nnd[nnd==0]) 
  total[3,i]<-length(nnd[nnd<1])
}

1 个答案:

答案 0 :(得分:1)

这应该有效:

for(i in 1:length(month)){
  mo <- month[i]
  m<-subset(eg, month==mo) #subsetting my data for each month
  mp<-ppp(m$LONG,m$LAT,window=W2) #creating a point pattern
  nnd<-nndist(mp) #calculating the nearest neighbour in a point pattern
  total[1,i]<-length(nnd[nnd>1]) #Filling up the matrix with results 1
  total[2,i]<-length(nnd[nnd==0]) 
  total[3,i]<-length(nnd[nnd<1])
}

作为替代方案(没有for循环):

library(spatstat)
W2 <- ripras(eg$LONG, eg$LAT)

dat <- setNames(rep(0, 3), c(1, 0, -1))

do.call(cbind,
        lapply(split(eg, eg$month, drop = TRUE),
               function(m) {
                 tab <- rev(table(sign(nndist(ppp(m$LONG, m$LAT, window = W2)))))
                 dat[match(names(tab), names(dat))] <- tab
                 return(dat)
               }))