变量值在一个循环结束时和下一个循环开始时不一致

时间:2015-02-28 03:10:47

标签: r rstudio

请参阅下面的代码,我从数据框中删除某些列,并使用名为'col'的变量跟踪我当前的列。

问题 如果我从数据框中删除一列,我会减少'col'的值,但是,这似乎没有显示效果。在打印日志时,我看到循环开始时col的值似乎没有反映变量'col'的减量

数据

col1 = c(1,2,3,4,NA)
col2 = c(2,3,NA,NA,NA)
col3 = c(NA,NA,NA,1,NA)
col4 = c(NA,NA,NA,NA,NA)
col5 = c(1,NA,NA,NA,NA)
col6 = c(NA,NA,NA,NA,1)
col7 = c(NA,NA,NA,NA,2)
col8 = c(NA,NA,NA,NA,8)
col9 = c(NA,NA,NA,NA,NA)
col10= c(1,2,3,4,5)
df = data.frame(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10)

代码

col = 0
totalcolumns = ncol(df)
for (col in 1:totalcolumns)
{
 cat(paste("value of col at the start of the loop==",col,"\n",sep=""))

 if(length(which(is.na(df[,col]))) == nrow(df))
 {
    cat(paste("all nas at col==",col,"\n",sep=""))
    cat(paste("removing column",col,"\n",sep=""))

    df[,col] = NULL

    col = col - 1 
    totalcolumns = totalcolumns - 1

    cat(paste("totalcolumns ==",totalcolumns," col==",col, "\n",sep=""))
 }

 cat(paste("value of col at the end of the loop==",col,"\n\n",sep=""))
}

输出

value of col at the start of the loop==1
value of col at the end of the loop==1

value of col at the start of the loop==2
value of col at the end of the loop==2

value of col at the start of the loop==3
value of col at the end of the loop==3

value of col at the start of the loop==4
all nas at col==4
removing column4
totalcolumns ==9 col==3
value of col at the end of the loop==3

value of col at the start of the loop==5
value of col at the end of the loop==5

value of col at the start of the loop==6
value of col at the end of the loop==6

value of col at the start of the loop==7
value of col at the end of the loop==7

value of col at the start of the loop==8
all nas at col==8
removing column8
totalcolumns ==8 col==7
value of col at the end of the loop==7

value of col at the start of the loop==9
Error in `[.data.frame`(df, , col) : undefined columns selected

请注意,循环第四次迭代结束时'col'的值为3,但是在第五次迭代开始时,它显示为5,我希望它显示为4

编辑: 正如Buckminster和MrFlick所说,减少for循环中'col'的值,R设计没有显示出任何影响。但这是件好事吗?见下面C和R之间的差异

R代码

> for(i in 1:9){print(i);if(i==9){i=i-1}}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9

C代码

#include <stdio.h>

int main(void)
{
 int i = 0;

 for(i=0;i<10;i++)
 {
  printf("value of i==[%d]\n",i);

  if(i == 9)
  {
   i--;
  }
 } 

 return 0;
}

输出

Will never terminate
1
2
3
4
5
6
7
9
9
...
infinitely printing
9
..

2 个答案:

答案 0 :(得分:1)

在下一次迭代开始时,对用作迭代器的变量值的任何更改都将丢失。因为您已经使用

启动了循环
for (col in 1:totalcolumns)

它将运行totalcolumns次,col每次增加一次。您尝试更改

col = col - 1 

将重置为下一次迭代。当您考虑像

这样的案例时,这更有意义
for(x in c("apple", "orange", "banana")) {
   x <- paste("i want your", x)
   print(x)
}

认为在循环中更改x会对迭代的进行方式产生任何影响是没有意义的。

答案 1 :(得分:0)

循环的问题在于此代码:for (col in 1:totalcolumns)。在每次循环迭代开始时,col将被赋予向量1:totalcolumns中的下一个值,而不管前一次迭代中发生了什么。