# [R] Help with for loops and if statements

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Thu Sep 24 11:01:48 CEST 2020

```Hi Dan,
This list has a "no homework" policy, but as you really do seem to be
struggling, I'll suggest that you read the help page for "abs"
carefully and learn about "<-" (assign a value). Perhaps these will
give you a start.

Jim

On Thu, Sep 24, 2020 at 6:19 PM Dan Bosio <djbosio14 using gmail.com> wrote:
>
> Hi there
>
> 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)
>
>
> (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!
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help