【问题标题】:How for loop works for a vector in R?for循环如何适用于R中的向量?
【发布时间】:2016-04-08 14:40:56
【问题描述】:

我试图了解 R 中向量的 for 循环的工作原理。我找到了解决问题的方法,但对其基本工作原理存有疑问。

在创建函数的过程中,我遇到了这个问题。问题是 for 循环正在遍历向量的元素,但直到某个索引。

## the output is partially complete, seems like it didn't loop through all the values however the loop counter is perfect
temp_vector<- c(1, NA,Inf, NaN,3,2,4,6,4,6,7,3,2,5,NaN, NA, 3,3,NaN, Inf, Inf, NaN, NA, 3,5,6,7)
ctr<- 0
for(i in  temp_vector){

  temp_vector[i]<- ifelse((!is.na(temp_vector[i])) & (!is.infinite(temp_vector[i])), temp_vector[i], 0  )
  ## replace the element of vector by 0 if they are Inf or NA or NaN
  ctr<- ctr+1
}
temp_vector
print(ctr)

# output
> temp_vector
[1]   1   0   0   0   3   2   4   6   4   6   7   3   2   5 NaN  NA   3   3 NaN Inf Inf NaN  NA   3   5   6   7
> print(ctr)
[1] 27

## this is generating correct output
temp_vector<- c(1, NA,Inf, NaN,3,2,4,6,4,6,7,3,2,5,NaN, NA, 3,3,NaN, Inf, Inf, NaN, NA, 3,5,6,7)
for(i in 1:length(temp_vector)){

  temp_vector[i]<- ifelse((!is.na(temp_vector[i])) & (!is.infinite(temp_vector[i])), temp_vector[i], 0  )
  ## replace the element of vector by 0 if they are Inf or NA or NaN
}
temp_vector

# output
> temp_vector
[1] 1 0 0 0 3 2 4 6 4 6 7 3 2 5 0 0 3 3 0 0 0 0 0 3 5 6 7

以下是我尝试过的几个 for 循环变体,它们产生不同的输出,我试图了解它的基本工作原理。如果您能对此有所了解,那将很有帮助。谢谢!

## variant-0
y <- c(2,5,3,9,8,11,6)
count <- 0
for (val in y) {
  if(val %% 2 == 0) 
    count = count+1
}
print(count)  

# output
[1] 3

## variant-1
x<- c(2,4,6,4,6,7,3,2,5,6)
for(i in x){

  x[i]<- ifelse(x[i]==6, NaN, x[i])
}
x

# output, Last element of the vector is not a NaN
[1]   2   4 NaN   4 NaN   7   3   2   5   6



## variant-2
x<- c(2,4,6,4,6,7,3,2,5,6)
ctr<- 0
for(i in x){

  x[i]<- ifelse(x[i]==6, NaN, x[i])
  ctr<- ctr+1
}
x
print(ctr)

# output, Note: Last element of the vector is not a NaN
> x
[1]   2   4 NaN   4 NaN   7   3   2   5   6
> print(ctr)
[1] 10



## variant-3
x<- c(2,4,6,4,6,7,3,2,5,6)
ctr<- 0
for(i in x){

  x[ctr]<- ifelse(x[ctr]==6, NaN, x[ctr])
  ctr<- ctr+1
}
x
print(ctr)


# output. Note: the counter is perfect
> x
[1]   2   4 NaN   4 NaN   7   3   2   5   6
> print(ctr)
[1] 10


## variant-4
x<- c(2,4,6,4,6,7,3,2,5,6)
ctr<- 0
for(i in x){

  i<- ifelse(i==6, NaN, i)
  ctr<- ctr+1
}
x
print(ctr)

# output
> x
[1] 2 4 6 4 6 7 3 2 5 6
> print(ctr)
[1] 10

【问题讨论】:

    标签: r for-loop vector


    【解决方案1】:

    考虑以下示例:

    > y <- c(2, 5, 3, 9, 8, 11, 6)
    

    for 循环遍历您提供的向量。在第一种情况下,您遍历向量 y 的元素:

    > for (val in y) {
    +     print(val)
    + }
    [1] 2
    [1] 5
    [1] 3
    [1] 9
    [1] 8
    [1] 11
    [1] 6
    

    在第二种情况下,您正在迭代向量1:length(y) 的元素,即c(1, 2, 3, 4, 5, 6, 7)

    > for (val in 1:length(y)) {
    +     print(val)
    + }
    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    [1] 6
    [1] 7
    

    您在上面的代码中混淆了这一点。希望这可以解决问题!

    【讨论】:

    • 这是一个有效的概念,我知道这一点,但是如果你运行前两个块,它不会循环遍历第一部分中的所有元素,但是可以正常工作直到它达到某个索引.
    • 您的第一个循环 for(i in temp_vector) 循环遍历向量 temp_vector 的元素。然后,您使用 i 访问您的部分矢量,例如这里!is.na(temp_vector[i])。在第一次迭代中,一切都很好,因为i = 1,但是在第二次迭代中i = NA 并且您多次使用i 来访问您的向量的一部分,您应该使用适当的索引来代替。
    【解决方案2】:

    您使用的 for 循环以这种方式工作(我将使用第一个变体,即 variant-0)

    这是正常定义部分

    y <- c(2,5,3,9,8,11,6)
    count <- 0
    

    这里是业务开始的地方:

    for (val in y) 
    

    这里,val 将包含向量 y 的值,这些值将在每次迭代中发生变化。

    例如:

    val for iteration 1: 2;
    val for iteration 2: 5;
    val for iteration 3: 3;
    

    等等。

    {
      if(val %% 2 == 0) 
        count = count+1
    }
    print(count) 
    

    所以,在这里,当 val 为偶数时 count 将增加,即迭代:1,5,7

    所以,count 的值为 3。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2021-12-01
      • 2016-08-16
      相关资源
      最近更新 更多