简体   繁体   English

R中向量的for循环如何工作?

[英]How for loop works for a vector in R?

I am trying to understand the working of for loop for a vector in R. I figured out the solution for my problem but left with a doubt about its fundamental working. 我试图了解R中向量的for循环的工作方式。我想出了解决我问题的方法,但对其基本工作方式持怀疑态度。

In the process of creating a function, I came across this problem. 在创建函数的过程中,我遇到了这个问题。 The problem is that the for loop is looping through the elements of the vector but till a certain index. 问题是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

Below are few variants of for loops that I tried which generate different output, I am trying to understand how it basically works. 以下是我尝试过的for循环的几种变体,它们会产生不同的输出,我试图了解它的基本工作原理。 It would be helpful, if you could shed some light on it. 如果您可以对它有所了解,将很有帮助。 Thanks! 谢谢!

## 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

Consider the following example: 考虑以下示例:

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

for loops over the vector you provide. for在您提供的向量上循环。 In the first case you iterate over the elements of vector y : 在第一种情况下,您遍历向量y的元素:

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

In the second case you are iterating over the elements of vector 1:length(y) , meaning c(1, 2, 3, 4, 5, 6, 7) : 在第二种情况下,您要遍历vector 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

You got this mixed up in your code above. 您在上面的代码中混淆了这一点。 Hope this clears things up! 希望这可以清除一切!

The for loop that you are using works in this way (I will take up the first variant ie variant-0) 您正在使用的for循环以这种方式工作(我将使用第一个变体,即variant-0)

This is normal definition part 这是正常定义的部分

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

Here is where the business begins: 这是业务开始的地方:

for (val in y) 

Here, val will contain values of vector y which will be changing in each iteration. 此处,val将包含向量y的值,该值将在每次迭代中更改。

For example: 例如:

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

and so on. 等等。

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

So, here, count will be incremented when val is even ie for iteration: 1,5,7 因此,在这里,当val为偶数时,count将增加,即用于迭代:1,5,7

So, value of count is 3. 因此,计数值为3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM