R Numbers from 1 to 100

R numbers from 1 to 100

Your mistake is looking for range, which gives you the range of a vector, for example:

range(c(10, -5, 100))

gives

 -5 100

Instead, look at the : operator to give sequences (with a step size of one):

1:100

or you can use the seq function to have a bit more control. For example,

##Step size of 2
seq(1, 100, by=2)

or

##length.out: desired length of the sequence
seq(1, 100, length.out=5)

How do I generate a list with a specified increment step?

Executing seq(1, 10, 1) does what 1:10 does. You can change the last parameter of seq, i.e. by, to be the step of whatever size you like.

> #a vector of even numbers
> seq(0, 10, by=2) # Explicitly specifying "by" only to increase readability
> [1] 0 2 4 6 8 10

How do you create vectors with specific intervals in R?

In R the equivalent function is seq and you can use it with the option by:

seq(from = 5, to = 100, by = 5)
# [1] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100

In addition to by you can also have other options such as length.out and along.with.

length.out: If you want to get a total of 10 numbers between 0 and 1, for example:

seq(0, 1, length.out = 10)
# gives 10 equally spaced numbers from 0 to 1

along.with: It takes the length of the vector you supply as input and provides a vector from 1:length(input).

seq(along.with=c(10,20,30))
# [1] 1 2 3

Although, instead of using the along.with option, it is recommended to use seq_along in this case. From the documentation for ?seq

seq is generic, and only the default method is described here. Note that it dispatches on the class of the first argument irrespective of argument names. This can have unintended consequences if it is called with just one argument intending this to be taken as along.with: it is much better to use seq_along in that case.

seq_along: Instead of seq(along.with(.))

seq_along(c(10,20,30))
# [1] 1 2 3

In a sequence of numbers 1:100, use loop to print in R

Adjusting the code from FizzBuzz in R to your requirements:

divisor <-
function(number, string) {
function(d) {
if (d %% number == 0) string else ""
}
}
mod3er <- divisor(2, "two")
mod5er <- divisor(5, "five")
fizzbuzz <-
function(i) {
res <- paste0(mod3er(i), mod5er(i))
ifelse(res == "", i, ifelse(res == "twofive", "both", res))
}
sapply(1:100, fizzbuzz)

Ouput in joodle.com:


[1] "1" "two" "3" "two" "five" "two" "7" "two" "9" "both"
[11] "11" "two" "13" "two" "five" "two" "17" "two" "19" "both"
[21] "21" "two" "23" "two" "five" "two" "27" "two" "29" "both"
[31] "31" "two" "33" "two" "five" "two" "37" "two" "39" "both"
[41] "41" "two" "43" "two" "five" "two" "47" "two" "49" "both"
[51] "51" "two" "53" "two" "five" "two" "57" "two" "59" "both"
[61] "61" "two" "63" "two" "five" "two" "67" "two" "69" "both"
[71] "71" "two" "73" "two" "five" "two" "77" "two" "79" "both"
[81] "81" "two" "83" "two" "five" "two" "87" "two" "89" "both"
[91] "91" "two" "93" "two" "five" "two" "97" "two" "99" "both"

Create a sequence of sequences of numbers

Use sequence.

sequence(5:1, from = 1:5)
[1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5

The first argument, nvec, is the length of each sequence (5:1); the second, from, is the starting point for each sequence (1:5).

Note: this works only for R >= 4.0.0. From R News 4.0.0:

sequence() [...] gains arguments [e.g. from] to generate more complex sequences.



Related Topics



Leave a reply



Submit