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に感謝します。for(i in seq_along(month))またはfor(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])
}