Reputation: 77
I am in an intro to R course and the professor has not been much help. One of the questions on the latest homework has me stumped. The question is below, along with my answers so far.
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)
My answers:
(1) count <- c(rep(0,4))
(2)
for (count in x) {
if (x > -1 & x < 1) {
print(count[1])
}
I know there is something wrong with my code for part one but we haven't gone over anything like this in class and I have struggled to find a video for something like this. Please point me in the right direction and let me know what mistakes I have made, thanks so much!
Upvotes: 2
Views: 106
Reputation: 21274
Each of the 4 slots in count
is supposed to keep track of whether a value in x
satisfies one of the 4 conditions listed (a.
through d.
).
If we were to speak it out loud, it'd go something like:
x
(you can do that with x[1]
). It's 1.1478911
. This satisfies condition b.
, so add a 1 to the "b.
counter", which is the second slot in count
, or count[2]
.x
(that's x[2]
)...(and so on, up to the last element in x
).To solve this task, you could just write out 10 statements, looking at each of the 10 elements in x
separately, and update count
on a case-by-case basis, but that is long and is hard to modify.
A for
-loop is kind of like making a template for the spoken-out-loud part above. So instead of saying, "Ok, now we're on Element 3, let's see what the deal is", you can instead say, "Ok, now we're on Element i
...", where i
is just a temporary variable, a placeholder that only exists for the life of the for
-loop. The i
placeholder automatically takes on the value of the element in the vector we're iterating over.
If it's for (i in 1:3)
then i
will be 1, then 2, then 3.
If it's for (letter in c("a", "b", "c"))
, then letter
will be "a", then "b", then "c".
So you can see that when you write for (count in x)
, that doesn't follow the rules of the for
-loop. It's true that we'll want to update count
at some point in the loop, but you've got it in the spot where our temporary placeholder is supposed to go. You can call that placeholder whatever you want, but i
is common when looping over numbers, by convention.
Here's an example: the following code will start i
at 1, and repeat the code inside the loop statement with new integers, until i
gets to 10:
for (i in 1:10) {
print(paste("i is", i, "and the i'th value of x is", x[i]))
}
That should be enough to get you over the part you're stuck on.
A couple of additional hints:
x
, you can use length(x)
(try it, you will see the output is 10). So instead of doing: for(i in 1:10)
, you can swap out 10 for length(x)
.count[3] <- count[3] + 1
adds 1 to whatever the current total is in the third element of count
.Good luck! Someone may post the answer to the whole problem, but if you want to work through each piece, I hope this is a good jump start for you.
Upvotes: 1
Reputation: 389175
You part one is correct. Maybe you can remove the initial c()
from it.
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]))
}
Note that there are better/efficient ways to do this but I think for the purpose of this exercise this is what your professor wants.
Upvotes: 1