[R] Help with for loops and if statements
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Thu Sep 24 11:29:08 CEST 2020
Hi
And above what Jim suggested, count is only a placeholder for results of
tasks a-d.
Cheers
Petr
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Dan Bosio
> Sent: Wednesday, September 23, 2020 11:00 PM
> To: r-help using r-project.org
> 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]]
>
> ______________________________________________
> R-help using r-project.org 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.
More information about the R-help
mailing list