exercise question

Hello,

I recently started learning R and I`m doing exercises.
I would appreciate it if you help me with this exercise.
this exercise wants me to get the following vector by using seq() and rep():
1,0,3,0,5,0,7,0,....,49
How should I do that?

What have you tried?

Hi,

I wrote this and I got an answer, but I did not use seq() and rep(), also this contains a warning message.

1:49 * 1:0
[1] 1 0 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0 21 0 23 0 25 0 27 0 29 0 31 0 33 0 35 0 37 0
[39] 39 0 41 0 43 0 45 0 47 0 49
Warning message:
In 1:49 * 1:0 :
longer object length is not a multiple of shorter object length

1 Like

Nice simple solution.

1:49 is the same as seq(1, 49), so just use that instead for this exercise.
You can safely ignore the warning. It is just telling you that it has nothing to multiply the very last 0 by. The length of 1:49 is not a multiple of 2, which is the length of 1:0.

If you must use rep(), just repeating 1:0 25 times will give you a vector (1, 0, 1, 0, 1, 0, ...) with a length of 50. Unfortunately, you need it to be of length 49 to multiply by the 1 to 49 vector. There are several ways to subset a vector to keep only the first 49 elements. You can also look at the help for rep(), particularly length.out.

2 Likes

Got it!
I appreciate your time and effort.
It helped me a lot.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.