for-loop语句的理解 [英] for-loop statement understanding

查看:176
本文介绍了for-loop语句的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一个愚蠢的问题,但我无法理解它背后的逻辑。

 问题1:
a = c(10,20,30)
b = c(15,30 ,45)
c = cbind(a,b)
for(i in 1:ncol(c))
{d [i] = c [i] +2}

print(d)
#错误d未定义

然后我想也许问题2:
d = matrix(NA, 3,2)#现在具有与c
相同的维数(i in 1:ncol(c)){d [i] = c [i] +2}

输出是

$ $ p $ [,1] [,2]
[1,] 12 NA
[2,] 22 NA
[3,]不适用

我无法解释上面的输出。我不明白为什么我在第3行拿到NA。

我也尝试了以下方法:

  for(i in 1:ncol(c)){d [,i] = c [,i] +2} 

然后我得到正确的答案,但是当我尝试以下时,我得到一个错误:

 问题3:
for(i in 1:ncol(c)){d [i] = c [,i] +2}
警告信息:
1:In d [i] = c [,i] + 2:
要替换的项数不是替换长度的倍数
2:在d [i] = c [,i] + 2中:
取代的项目数不是取代长度的倍数

我知道我已经问过很多的问题,但如果你可以回答他们的虚拟,我会很感激。

解决方案

我知道回答你自己的问题是一个不好的做法,但是我想我可以给出一个非常虚拟的答案。广泛的搜索。

 问题1:
如前所述,这会给出如下错误:
# [i] = c [i] + 2:找不到对象'd'

为什么?
因为d不存在,所以当c [i]等于1时,即为10,并且加上2,给出12.结果不能放在d处,因为d不存在即没有定义。为了解决这个问题,你需要定义d,这就是我在问题2中所做的。定义意味着在运行for-loop语句之前,你需要创建一个空的地方来存储结果,而不是这里的情况,所以R会产生一个错误。

 问题2:
d = matrix(NA,3,2)#现在与c $ b具有相同的维度$ b for(i in 1:ncol(c)){d [i] = c [i] +2}
#[1] 12 22 NA NA NA

为什么?这里1:ncol(c)等于1& 2,因为只有2列a& b。当c [1]等于10时,c [2]等于20.这是索引而不是列。换句话说,列a包含(10,20,30),但是c [1]是列a的第一个元素,它是10,c [2]是第二个元素,等于20。这与c [,1]不同(查看1之前的逗号),这会给你一个列a的向量,即(10,20,30)。代码将如下工作:

 当我等于1时,c将等于10 
第一个现在我= 2
d [2] = 20 + 2
d = 2 $ bd = 22

因为我只有列1& 2即我只能是1& 2,剩余的输出将是NA。

 问题3:
for(i in 1:ncol(c)){d [i] = c [ ,i] +2}
#警告消息:
#1:在d [i] = c [,i] + 2中:
要替换的项目数不是替换长度
#2:在d [i] = c [,i] + 2中:
#要替换的项目数不是替换长度的倍数
#[1] 12 17不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用1和第2列,但是d [i]是索引,即当i= 1时,d [i]将是第一个元素,因为我们只有2列,那么答案的其余部分将是NA。看看它是如何工作的,让我们通过代码

 当我等于1那么c [,1] = 10 
d [i] = 10 +2 = 12
当i等于2时,结果12将被放置在矩阵d
的第一个元素中,则c [,2] = 15
d [i] = 15 + 2 = 17
结果17将被放置在矩阵的第二个元素d

所以输出结果如下:

pre $ $ $ $ $ $ $ $ b [1,] 12 NA
[2,] 17 NA
[3,]不适用
$ / code>

还要注意数字17的位置。这是第2列的第一个元素的结果,但它被放置在矩阵d的第二个元素中。这是非常危险的

I have probably a stupid question, but I just can not understand the logic behind it.

     Question 1:
     a = c(10,20,30)
     b = c(15,30,45)
     c = cbind(a,b)
     for ( i in 1:ncol(c))
          {d[i] = c[i]+2}

     print(d)
     # Error d not defined

Then I thought maybe I need a placeholder for the question, so I did the followings:

        Question 2: 
        d = matrix(NA, 3,2) # now have the same dimension as c         
        for ( i in 1:ncol(c)){d[i] = c[i]+2}

the output is

           [,1] [,2]
      [1,]   12   NA
      [2,]   22   NA
      [3,]   NA   NA

I cannot interpret the above output. I do not understand why I get NA in row 3.

I also tried the followings:

      for ( i in 1:ncol(c)){d[,i] = c[,i]+2}

then I get the right answer, but when I tried the following I get an error:

      Question 3: 
      for ( i in 1:ncol(c)){d[i] = c[,i]+2}
      Warning messages:
      1: In d[i] = c[, i] + 2 :
         number of items to replace is not a multiple of replacement length
      2: In d[i] = c[, i] + 2 :
         number of items to replace is not a multiple of replacement length

I know I have asked a lot of questions, but if you can answer them for a dummy , I will appreciate it a lot.

解决方案

I know it is a bad practice to answer your own question, but I think I can give a very dummy answer after an extensive search.

       Question 1: 
       As previous mentioned this will give an error as followings: 
       #Error in d[i] = c[i] + 2 : object 'd' not found

Why? Because "d" does not exists, so when c[i] is equal to 1, that is 10, and adds 2, giving 12. The result cannot be placed at "d", because "d" does not exists i.e. not defined. To solve the problem, you need to define "d" and that is what I did in question 2. Defining means that before running the for-loop statement you need to create an empty place where the results can be stored, which is not the case here, hence R will produce an error.

       Question 2:
       d = matrix(NA, 3,2) # now have the same dimension as c         
       for ( i in 1:ncol(c)){d[i] = c[i]+2} 
       #[1] 12 22 NA NA NA NA

Why? Here 1:ncol(c) is equal to 1 & 2, because are only 2 columns a&b. When c[1] is equal to 10, c[2] is equal to 20. This is indexing and not columns. In other words column "a" contains (10,20,30), but c[1] is the first element of the column "a" which is 10 and c[2] is the second element which is 20 and so on. This is different from c[,1] (look at the comma before 1), which will give you a vector of column "a" i.e. (10,20,30). The code will work as followings:

        when i is equal to 1 then c will be equal to 10
        the first element of matrix d will be equal to 
        d[1] = 10 +2 
        d = 12 continuing.... now i = 2
        d[2] = 20 +2
        d= 22

since I only have columns 1 & 2 that is "i" can only be 1 & 2, the remaining output will be NA.

       Question 3: 
       for ( i in 1:ncol(c)){d[i] = c[,i]+2}
       #Warning messages:
       # 1: In d[i] = c[, i] + 2 :
         #number of items to replace is not a multiple of replacement length
       #2: In d[i] = c[, i] + 2 :
         #number of items to replace is not a multiple of replacement length
       #[1] 12 17 NA NA NA NA 

Here the main issue is that c[,i] will take the elements of column 1 and column 2, but d[i] is index i.e. when "i" = 1, then d[i] will be the first element and since we only have 2 columns, then rest of the answer will be NA. To see how it works let us go through the code

     when i is equal to 1 then c[,1] = 10
     d[i] = 10 +2 = 12
     The result 12 will be placed in the first element of matrix d
     when i is equal to 2 then c[,2] = 15
     d[i] = 15+2 = 17
     The result 17 will be placed in the second element of the matrix d

So the output will look as followings:

               [,1] [,2]
         [1,]   12   NA
         [2,]   17   NA
         [3,]   NA   NA   

Also notice the placement of the number 17. This the result of first element of column 2, but it is placed in the second element of matrix d. This is very dangerous.

这篇关于for-loop语句的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆