经过频繁的搜索和大量的论坛阅读,我觉得我现在应该已经找到/理解了这个问题的答案,但这仍然让我感到困惑。我在 r 中有两个嵌套的 for 循环,需要将输出保存到一个变量中,但我不完全知道要分配什么以及在哪里分配。代码按我的意愿运行,只是我似乎无法找到如何以一种或另一种形式获得输出。循环的输入是子矩阵列表。输出可以是相同的格式,但会包含循环中发生的变化,或者更理想的格式会包含一个矩阵中的所有行和列。我尝试做一个 cbind 以及在循环之外创建变量以便稍后存储所有内容(你可能会注意到我在这些方面的注释尝试),但是,就像我说的,我 米还是有点糊涂。任何帮助将不胜感激!
loop.List <- list()
#results.frame <- data.matrix()
ctr <- 0 # creating a counter and zeroing it
for (i in 1:length(subM.List)) { #Looping through each submatrix in the list
loop.List[[i]] <- list()
for (j in 2:nrow(subM.List[[i]])){ #Loop through each row of each submatrix in the list
if ((subM.List[[i]][j, "LAT"] == -180) & #Imputation 1, imputing data points with 0 activity intensity
(subM.List[[i]][j, "ACTIVITYIN"] == 0) &
!((subM.List[[i]][j-1, "ACTIVITYIN"] > 0))[1]) { #stop imputing at the first occurrence of a value > 0 in activity intensity
imputeLat <- replace(subM.List[[i]][ ,"LAT"], subM.List[[i]][j,"LAT"], subM.List[[i]][j-1,"LAT"])
imputeLon <- replace(subM.List[[i]][ ,"LON"], subM.List[[i]][j,"LON"], subM.List[[i]][j-1,"LON"])
replace.col <- replace(subM.List[[i]][ ,"Impute"], subM.List[[i]][j,"Impute"], 1) #populated impute column. If point is imputed will have a 1
allComb <- cbind(imputeLat, imputeLon, replace.col)
ctr <- (ctr + 1)
}
}
#result[[i]] <- allComb
#write.table(results.frame, "C:\\RWorkspace\\newMatrix.txt")
#return(results.frame)
}
编辑:子矩阵之一的数据样本 该列表包含几个具有不同行数的子矩阵。
FixTYPE ActivityIn LAT LON Impute
8 0 32.81320 -117.2300
8 0 32.81324 -117.2301
8 1 32.81327 -117.2302
8 1 32.81326 -117.2301
6 0 32.81324 -117.2300
6 0 32.81338 -117.2302
6 0 32.81353 -117.2299
7 0 -180.000 -180.000
7 0 -180.000 -180.000
7 0 -180.000 -180.000
7 0 -180.000 -180.000
7 1 -180.000 -180.000
7 2 -180.000 -180.000
7 1 -180.000 -180.000
1 0 32.81315 -117.2300
8 0 32.81318 -117.2300