Help with for loops and if statements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with for loops and if statements

farmerDan
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)

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!
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Help with for loops and if statements

Jim Lemon-4
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 <[hidden email]> 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)
>
> 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!
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Help with for loops and if statements

PIKAL Petr
In reply to this post by farmerDan
Hi

And above what Jim suggested, count is only a placeholder for results of
tasks a-d.

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Dan Bosio
> Sent: Wednesday, September 23, 2020 11:00 PM
> To: [hidden email]
> Subject: [R] Help with for loops and if statements
>
> 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)
>
> 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!
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.