Obtaining a numeric vector

I am learning how to do vectorization. I get the basics for the most part, but am having trouble obtaining a particular output:

2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4 (numeric vector of length 20)

There's a few limitations: not using c() or , and try using rep() or seq().

Any guidance would be appreciated, thanks!

Can you add some more detail of your input and how you're hoping to achieve the output?

Yes! Let me show you what I have tried thus far.

rep(1:5, times = 5) + rep(0:4, each = 5)
[1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9

This is a step in the right direction, but I need the limit to not go higher than the number 5.
Next step after that, is to figure out how to remove subsequent numbers in each block of numbers.

It is still not really clear what you want to achieve. If you want a vector to contain 20 items which are slightly random from 1:5 then why can't the following suffice?

df <- sample(1:5,20,replace = TRUE)
df
#>  [1] 2 2 1 4 5 5 5 1 2 1 5 5 5 4 2 5 4 2 3 2

Created on 2020-09-21 by the reprex package (v0.3.0)

Sorry I wasn't clear enough. From my original post, that is the set of numbers I want. So I need to create an input that comes up with the output "2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4".

I have an answer that uses rep(), seq() and the [ operator. Is use of [ disallowed?
Also, this isn't homework, is it?

I just went back and read the FAQs and changed it to a homework tag because it is. My apologies, should have clarified and been informed. However, it is an example not the exact question from an assignment.
Again, not looking for just an answer because that's not the point. I'm aiming to learn the language, not get the best grade.

A solution in constant time:

c(2, 3, 4, 5, 1, 3, 4, 5, 1, 2, 4, 5, 1, 2, 3, 5, 1, 2, 3, 4)

More seriously:

c(sapply(0:3, function(i) ((1:5) + i) %% 5)) + 1
2 Likes

I will hint at my solution. The final vector is 1:5 repeated 5 times except the elements at positions 1, 7, 13, 19, and 25 have been removed. Think about the pattern of those indices and what notation is used to remove an element from a vector using [.

1 Like

@ChrisL - please tell me how you came up with that. I'm impressed!

Awesome, that makes sense when I take a second look. As far as remove functions go, can I use the rm() or remove()?

Digging around, it looks like this is getting me somewhere.

> x <- 1:5
> x2 <- x[!x %in% 1]
> x2
[1]  2 3 4 5

It would only be for first block of numbers, but does what I am looking for. Let me know your thoughts and appreciate the help!

thanks! I saw the repeatedly rotated 1:5, which lends itself to the mod operation, that's all.

Try reviewing how you index elements of a vector if you want to exclude some. To get the 5th element you write x[5]. How do you get everything except the 5th element?

1 Like

How does this look? Not sure it's the correct format for my purposes but it does get me to the correct answer.

> rep(seq(1,5, by = 1), times = 5)
 [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
> x <- rep(seq(1,5, by = 1), times = 5)
> x[-1][-7][-13][-19][-25]
 [1] 2 3 4 5 1 2 4 5 1 2 3 4 1 2 3 4 5 1 3 4 5

Can you build your set of negative numbers using seq()?

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.