编辑 摘要:我的问题是,当我通过嵌套的 for 循环运行四个数据帧的列表时(它可以正常工作并且正在做我需要做的事情),我只能将输出放入矩阵的三列中我更喜欢为我的内部 for 循环的每个参数设置一个列。我认为循环正在为列表中的每个项目创建一个列(请参见下面的输出),因为当我尝试将输出存储为单个列时,我收到此错误:维度数不正确。我需要列中的数据而不是将其存储在循环外创建的整个矩阵中的原因是因为将来我将运行不同的文件并且不知道循环将生成的行数。我真的很感激这方面的帮助,我
这可能是一个很容易解决的问题,但我一直没有找到答案。我已经嵌套了 for 循环,我的输入是四个数据帧的列表(尽管这个数字可能并且会随着不同的数据集而变化)。重要的是我的输入保持在一个列表中。到目前为止,我已经创建了矩阵来存储这些输出,并且输出最终进入矩阵的三个单独的列,我假设这是因为它是列表中每个项目的一列(我知道我在列表,但代码最终只对列表中四项中的三项进行更改)。
LAT <- matrix(data=NA, nrow=50, ncol=5) #Matrices to store the output in
LON <- matrix(data=NA, nrow=50, ncol=5) #but ideally these would be columns: LON <- c()
Impute <- matrix(data=NA, nrow=50, ncol=5)
ID <- matrix(data=NA, nrow=50, ncol=5)
for (i in 1:length(subM.List)) { #Looping through each submatrix in the list (i loops through each submatrix on list)
for (j in 2:nrow(subM.List[[i]])) { #Loop through each row of each submatrix in the list (j loops through each row on each submatrix)
if ((subM.List[[i]][j, "LAT"] == -180) & #if there is a -180 and a value greater than 0 in activityIN, break the inner for loop
(subM.List[[i]][j-1, "ACTIVITYIN"] != 0)) { #Will break if it find a -1, -2, 1, 2, 3, or 4.
break
}
else if ((subM.List[[i]][j, "LAT"] == -180) &
(subM.List[[i]][j, "ACTIVITYIN"] == 0)) {
ID[j,i] <- (subM.List[[i]][j,"ID"]) #Imputation 1, imputing data points with 0 activity intensity and -180 for LON
LAT[j,i] <- (subM.List[[i]][j,"LAT"] = subM.List[[i]][j-1,"LAT"])
LON[j,i] <- (subM.List[[i]][j,"LON"] = subM.List[[i]][j-1,"LON"])
Impute[j,i] <- (subM.List[[i]][j,"Impute"] = 1) #populates the impute column. If point has been imputed, row will have a 1 value
ctr <- (ctr + 1)}
}
}
loopOutput = cbind(LAT, LON, Impute, ID) #binding together the 4 columns of the loop output
LON 变量的当前输出如下所示:
> LON
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] NA NA NA NA
[3,] NA NA NA NA
[4,] NA NA NA NA
[5,] NA NA NA NA
[6,] NA NA NA NA
[7,] NA NA NA NA
[8,] NA NA NA NA
[9,] NA NA NA NA
[10,] NA NA NA NA
[11,] NA NA NA NA
[12,] NA -117.2295 -117.2289 -117.2295
[13,] NA -117.2295 -117.2289 -117.2295
[14,] NA -117.2295 -117.2289 -117.2295
[15,] NA NA -117.2289 -117.2295
[16,] NA NA -117.2289 -117.2295
[17,] NA NA -117.2289 -117.2295
[18,] NA NA -117.2289 -117.2295
[19,] NA NA -117.2289 -117.2295
[20,] NA NA -117.2289 -117.2295
[21,] NA NA -117.2289 -117.2295
[22,] NA NA -117.2289 -117.2295
[23,] NA NA -117.2289 -117.2295
[24,] NA NA -117.2289 -117.2295
[25,] NA NA -117.2289 -117.2295
[26,] NA NA -117.2289 NA
[27,] NA NA -117.2289 NA
[28,] NA NA -117.2289 NA
[29,] NA NA -117.2289 NA
[30,] NA NA -117.2289 NA
[31,] NA NA -117.2289 NA
[32,] NA NA -117.2289 NA
[33,] NA NA -117.2289 NA
[34,] NA NA -117.2289 NA
[35,] NA NA -117.2289 NA
[36,] NA NA -117.2289 NA
[37,] NA NA -117.2289 NA
[38,] NA NA -117.2289 NA
[39,] NA NA -117.2289 NA
[40,] NA NA -117.2289 NA
[41,] NA NA -117.2289 NA
[42,] NA NA -117.2289 NA
[43,] NA NA NA NA
[44,] NA NA NA NA
[45,] NA NA NA NA
首先,我希望输出是每个变量的单列,而不是数据框或矩阵(然后在循环外执行我的 cbind )。另外,我只想要数字输出,通过循环实际更改的项目,不包括任何 NA(尽管我意识到现在大多数 NA 来自我的矩阵维度)。因此,例如,我的 LON 变量将是一个单列,其中包含循环产生的所有数字行。我想要的输出的一个小样本如下所示:
cbind 之后的所有列:
LAT LON Impute ID
[475,] 32.81331 -117.2295 1 1021
[476,] 32.81331 -117.2295 1 1022
[477,] 32.81331 -117.2295 1 1023
[478,] 32.81331 -117.2295 1 1148
[479,] 32.81331 -117.2295 1 1149
[480,] 32.81331 -117.2295 1 1150
[481,] 32.81331 -117.2295 1 1151
[482,] 32.81331 -117.2295 1 1152
[483,] 32.81331 -117.2295 1 1153
[484,] 32.81331 -117.2295 1 1154
[485,] 32.81331 -117.2295 1 1155
[486,] 32.81331 -117.2295 1 1156
[487,] 32.81331 -117.2295 1 1157
[488,] 32.81331 -117.2295 1 1158
最后,这是我的列表(四个列表)中第二个数据框的示例,它代表输入:
[[2]]
FIXTYPE LON LAT ACTIVITYIN Impute ID
537 6 -117.2295 32.81602 0 NA 537
538 6 -117.2295 32.81602 0 NA 538
539 6 -117.2295 32.81602 0 NA 539
540 6 -117.2295 32.81602 0 NA 540
541 6 -117.2295 32.81602 0 NA 541
542 6 -117.2295 32.81602 0 NA 542
543 6 -117.2295 32.81602 0 NA 543
544 6 -117.2295 32.81602 0 NA 544
545 6 -117.2295 32.81602 0 NA 545
546 6 -117.2295 32.81602 0 NA 546
547 6 -117.2295 32.81602 0 NA 547
548 7 -180.0000 -180.00000 0 NA 548
549 7 -180.0000 -180.00000 0 NA 549
550 7 -180.0000 -180.00000 0 NA 550
551 7 -180.0000 -180.00000 1 NA 551
552 7 -180.0000 -180.00000 1 NA 552
另外,对不起,这太长了,我只是想非常清楚我的输入、输出和期望的输出。任何帮助都会非常感激,因为我已经在这个脚本上努力了一段时间。
编辑:由于评论,下面是有关输入(subM.List)的更多详细信息,我已将上面输入的示例更改为列表中的第二项,实际上将在循环中更改。
str(subM.List)
List of 4
$ : num [1:11, 1:6] 5 5 5 5 5 5 5 5 5 5 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:11] "452" "453" "454" "455" ...
.. ..$ : chr [1:6] "FIXTYPE" "LON" "LAT" "ACTIVITYIN" ...
$ : num [1:137, 1:6] 6 6 6 6 6 6 6 6 6 6 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:137] "537" "538" "539" "540" ...
.. ..$ : chr [1:6] "FIXTYPE" "LON" "LAT" "ACTIVITYIN" ...
$ : num [1:315, 1:6] 5 5 5 5 5 5 5 5 5 5 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:315] "718" "719" "720" "721" ...
.. ..$ : chr [1:6] "FIXTYPE" "LON" "LAT" "ACTIVITYIN" ...
$ : num [1:26, 1:6] 5 5 5 5 5 5 5 5 5 5 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:26] "1134" "1135" "1136" "1137" ...
.. ..$ : chr [1:6] "FIXTYPE" "LON" "LAT" "ACTIVITYIN" ...