【问题标题】:Can't figure out how to format a for loop with if statements无法弄清楚如何使用 if 语句格式化 for 循环
【发布时间】:2020-09-24 01:20:05
【问题描述】:

我正在学习 R 课程,教授并没有提供太多帮助。最新作业中的一个问题让我很难过。问题如下,以及我到目前为止的答案。

8. [15 points] Given the following code,
#
# x <- rnorm(10)
#
# Do the following.
#
# (1) create a count vector named "count" of four elements and set each to 0 using the rep function.
# (2) using a for loop to process each value in the vector x, count how many times each of the following values occur in the vector x using an if statement.
# a. "value is between -1 and 1 inclusive"
# b. "value is between -2 and 2 inclusive, but not between -1 and 1",
# c. "value is between -3 and 3 inclusive, but not between -2 and -2", or
# d. "value is greater than 3 or less than -3".
# (3) print each of the four counts in the count vector using a while loop.
#
# For example, if the vector x contains the following ten values,
#
# 1.1478911  1.6183994 -2.3790632 -0.2566993  0.8923735
# -0.7523441 -0.7559083  0.9836396  1.0994189  2.5519972
#
# Then, the output should be as below.
#
# count[1] is 5
# count[2] is 3
# count[3] is 2
# count[4] is 0

x <- rnorm(10)

我的回答:

(1)count &lt;- c(rep(0,4))

(2)

for (count in x) {
          if (x > -1 & x < 1) {
                 print(count[1])

}

我知道我的第一部分代码有问题,但我们在课堂上还没有讨论过这样的事情,我一直在努力寻找这样的视频。请指出正确的方向,让我知道我犯了什么错误,非常感谢!

【问题讨论】:

    标签: r


    【解决方案1】:

    你的第一部分是正确的。也许您可以从中删除最初的c()

    x <- rnorm(10)
    #Part 1
    count <- rep(0,4)
    
    #Part 2
    for(i in x) {
      if(i >= -1 && i <= 1)
        count[1] <- count[1] + 1
      else  if(i >= -2 && i <= 2)
        count[2] <- count[2] + 1
      else if(i >= -3 & i <= 3)
        count[3] <- count[3] + 1
      else count[4] <- count[4] + 1
    }
    
    #Part 3
    i <- 0
    while (i < length(count)) {
      i <- i + 1
      print(sprintf('count[%d] is: %d', i, count[i]))
    }
    

    请注意,有更好/更有效的方法可以做到这一点,但我认为出于本练习的目的,这正是您的教授想要的。

    【讨论】:

      【解决方案2】:

      count 中的 4 个插槽中的每一个都应该跟踪 x 中的值是否满足列出的 4 个条件之一(a.d.)。

      如果我们要大声说出来,它会是这样的:

      • 查看x 中的元素1(您可以使用x[1] 做到这一点)。这是1.1478911。这满足条件b.,因此将1 加到“b. 计数器”,这是countcount[2] 中的第二个槽。
      • 现在查看x 中的元素2(即x[2])...(依此类推,直到x 中的最后一个元素)。

      要解决这个任务,你可以写出 10 条语句,分别查看 x 中的 10 个元素中的每一个,并根据具体情况更新 count,但这很长而且很难修改。

      for-loop 有点像为上面的大声朗读部分制作模板。因此,您可以说,“好的,现在我们在 Element i...”,i只是一个临时变量,一个仅在for-loop 的生命周期内存在的占位符。 i 占位符自动采用我们正在迭代的向量中元素的值。

      如果是 for (i in 1:3),那么 i 将是 1,然后是 2,然后是 3。
      如果是for (letter in c("a", "b", "c")),那么letter 将是“a”,然后是“b”,然后是“c”。

      因此您可以看到,当您编写for (count in x) 时,它不遵循for 循环的规则。确实,我们希望在循环中的某个时间点更新count,但是您已经在我们的临时占位符应该去的地方得到了它。您可以随意调用该占位符,但按照惯例,i 在循环数字时很常见。

      这是一个示例:以下代码将从 1 开始 i,并在循环语句中使用新整数重复代码,直到 i 达到 10:

      for (i in 1:10) {
        print(paste("i is", i, "and the i'th value of x is", x[i]))
      }
      

      这应该足以让你克服你被困的部分。

      一些额外的提示:

      1. 如果你想知道一个向量中有多少东西,比如x,你可以使用length(x)(试试看,你会看到输出是10)。因此,您可以将 10 换成 length(x),而不是:for(i in 1:10)
      2. count[3] &lt;- count[3] + 1count 的第三个元素中的当前总数加 1。

      祝你好运!有人可能会发布整个问题的答案,但如果您想完成每一个部分,我希望这对您来说是一个很好的起点。

      【讨论】:

      • 非常感谢您的帮助。我认为我在“count in x”部分有问题,教授甚至从未在 for 循环中提到任何关于 i 的内容。在查看另一个用户给出的答案之前,我会尝试自己完成它,因为我确信这是我需要学习的东西。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2021-08-24
      • 2013-10-07
      • 2023-03-25
      相关资源
      最近更新 更多