2012-11-08 98 views
1

我試圖用for循環計算結果來填充空矩陣。我不知道如何在我的for循環中指定索引,即現在我的for循環代碼不起作用,並且我無法將結果導入到我的矩陣中。for循環和填充結果R中的空矩陣

這裏是我的數據的一個子集:

> 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.24), 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 

EDITS :

謝謝@SvenHohenstein,我需要使用(我在seq_along(月))或(我在1:長度(月)),但問題是,我的月份是因素與水平「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]) 
} 
+0

我不明白你想解決什麼問題。請更明確地陳述你的問題。 –

+0

看到編輯,希望這可以澄清我想要做的事情。 – GodinA

+1

我想如果你用'for(我在seq_along(month))'和'subset(例如,month == i)'''替換'subset(例如,month == unique) (月[I]))'。 –

回答

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) 
       })) 
+0

謝謝@Sven Hohenstein!這很好。 – GodinA