首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对于R中的向量,循环是如何工作的?

对于R中的向量,循环是如何工作的?
EN

Stack Overflow用户
提问于 2016-04-08 14:40:56
回答 2查看 7.1K关注 0票数 1

在R中,我试图理解向量的for循环的工作,我找到了我的问题的解决方案,但对它的基本工作留下了疑问。

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

代码语言:javascript
运行
复制
## 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循环的几个变体,我试图了解它的基本工作原理。如果你能弄清楚这件事,那会很有帮助的。谢谢!

代码语言:javascript
运行
复制
## 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-08 14:54:15

请考虑以下示例:

代码语言:javascript
运行
复制
> y <- c(2, 5, 3, 9, 8, 11, 6)

for循环在您提供的向量上。在第一种情况下,迭代向量y的元素。

代码语言:javascript
运行
复制
> 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)

代码语言:javascript
运行
复制
> for (val in 1:length(y)) {
+     print(val)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7

您在上面的代码中混淆了这一点。希望这能把事情弄清楚!

票数 3
EN

Stack Overflow用户

发布于 2016-04-08 14:57:41

以这种方式使用的for循环(我将使用第一个变量,即变量-0)

这是正常的定义部分。

代码语言:javascript
运行
复制
y <- c(2,5,3,9,8,11,6)
count <- 0

这里是生意的起点:

代码语言:javascript
运行
复制
for (val in y) 

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

例如:

代码语言:javascript
运行
复制
val for iteration 1: 2;
val for iteration 2: 5;
val for iteration 3: 3;

诸若此类。

代码语言:javascript
运行
复制
{
  if(val %% 2 == 0) 
    count = count+1
}
print(count) 

因此,在这里,当val为偶数时,计数将增加,即迭代: 1,5,7

因此,计数值为3。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36502503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档