如何循环包含NA值的列?环R

时间:2015-10-19 19:48:22

标签: r loops for-loop na

我有以下数据,如下所示:

  2010-11-04 to 2010-11-10 2010-11-11 to 2010-11-17 2010-11-18 to 2010-11-24 2010-11-26 to 2010-12-02
 [1,] "Stock194"               "Stock194"               NA                       "Stock241"              
 [2,] "Stock26"                "Stock217"               NA                       "Stock440"              
 [3,] "Stock314"               "Stock241"               NA                       "Stock485"              
 [4,] "Stock334"               "Stock321"               NA                       "Stock57"               
 [5,] "Stock453"               "Stock334"               NA                       "Stock77"               
 [6,] "Stock62"                "Stock39"                NA                       NA                      
 [7,] NA                       "Stock391"               NA                       NA                      
 [8,] NA                       "Stock430"               NA                       NA                      
 [9,] NA                       "Stock444"               NA                       NA                      
[10,] NA                       "Stock453"               NA                       NA                      
[11,] NA                       "Stock485"               NA                       NA                      
[12,] NA                       "Stock62"                NA                       NA                      
[13,] NA                       "Stock76"                NA                       NA                      
[14,] NA                       NA                       NA                       NA                      
[15,] NA                       NA                       NA                       NA                      
[16,] NA                       NA                       NA                       NA                      
[17,] NA                       NA                       NA                       NA                      
[18,] NA                       NA                       NA                       NA                      
[19,] NA                       NA                       NA                       NA                      
[20,] NA                       NA                       NA                       NA                      
[21,] NA                       NA                       NA                       NA                      
[22,] NA                       NA                       NA                       NA                      
      2010-12-03 to 2010-12-09
 [1,] "Stock12"               
 [2,] "Stock217"              
 [3,] "Stock321"              
 [4,] "Stock334"              
 [5,] "Stock39"               
 [6,] "Stock391"              
 [7,] "Stock453"              
 [8,] "Stock464"              
 [9,] "Stock485"              
[10,] "Stock62"               
[11,] "Stock76"               
[12,] NA                      
[13,] NA                      
[14,] NA                      
[15,] NA                      
[16,] NA                      
[17,] NA                      
[18,] NA                      
[19,] NA                      
[20,] NA                      
[21,] NA                      
[22,] NA  

> dput(a)
structure(c("Stock194", "Stock26", "Stock314", "Stock334", "Stock453", 
"Stock62", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "Stock194", "Stock217", "Stock241", "Stock321", "Stock334", 
"Stock39", "Stock391", "Stock430", "Stock444", "Stock453", "Stock485", 
"Stock62", "Stock76", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, "Stock241", "Stock440", "Stock485", "Stock57", 
"Stock77", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "Stock12", "Stock217", "Stock321", "Stock334", 
"Stock39", "Stock391", "Stock453", "Stock464", "Stock485", "Stock62", 
"Stock76", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(22L, 
5L), .Dimnames = list(NULL, c("2010-11-04 to 2010-11-10", "2010-11-11 to 2010-11-17", 
"2010-11-18 to 2010-11-24", "2010-11-26 to 2010-12-02", "2010-12-03 to 2010-12-09"
)))
> 

我也有一个如下所示的循环:

for (i in names(results2)) {
input <- i

# other steps and calculations 

}

我需要循环做的是例如在第一步中抓取整个列的行1到n并将其放入输入 - 包括NA值。

问题在于,由于某种原因,名称(results2)不会读取列。我认为这是因为NA值存在,但我不确定。

1 个答案:

答案 0 :(得分:2)

我认为你想要做的是循环每一列。按索引而不是按名称访问矩阵。

for (i in (1:ncol(results2))) {
 input <- results2[,i]   
 # other steps and calculations 
}

它似乎与列名无关,因为您的列具有有效且唯一的名称。在这种情况下,如果你愿意的话,你可以通过名字访问,因为@Frank提到它可能不稳定。

for (i in colnames(results2)) {
 input <- results2[,i]   
 # other steps and calculations 
}