【发布时间】: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 <- c(rep(0,4))
(2)
for (count in x) {
if (x > -1 & x < 1) {
print(count[1])
}
我知道我的第一部分代码有问题,但我们在课堂上还没有讨论过这样的事情,我一直在努力寻找这样的视频。请指出正确的方向,让我知道我犯了什么错误,非常感谢!
【问题讨论】:
标签: r