Barbie Janssens
Posted Questions
No Question(s) posted yet!
Posted Answers
Answer
In this R tutorial, you will learn how to generate sequences of numbers in R. There are many reasons why we would want to generate sequences of numbers. For example, we may want to generate sequences when plotting the axes of figures or simulating data.
As often there is no one way to perform a specific task in R. In this post, we are going to use the : operator, the seq(), and rep() functions. First, we start having a look at the : operator. Second, we dive into the seq() function including the arguments that we can use. Third, we will have a look at how we can use the rep() function to generate e.g. sequences of the same numbers or a few numbers.
The absolutely simplest way to create a sequence of numbers in R is by using the : operator. Here’s how to create a sequence of numbers, from 1 to 10:
As you can see, in the image above, that gave us every integer from 1 and 10 (an integer is every positive or negative counting number, including 0). Furthermore, the created sequence is in ascending order (i.e., from the smallest number to the largest number). We will soon learn how to generate a sequence in descending order. First, however, if we want our sequence, from 1 to 10, to be saved as a variable we have to use the <- and create a vector:
Now, you might already have guessed that we can just change the order of the smallest and largest numbers to generate a sequence of numbers in descending order:
Note, that if you want to know more about a particular R function, you can access its documentation with a question mark followed by the function name: ?function_name_here.
In the particular case, however, an operator like the colon we used above, you must enclose the symbol in backticks like this: ?’:’. Before we go to the next section it is worth mentioning that you can also use R to transpose a matrix or a dataframe.
Often, we desire more control over a sequence we are creating than what the : operator will give us. The seq() function serves this purpose and is a generalization of the : operator, which creates a sequence of numbers with a specified arithmetic progression.
Now, the most basic use of seq(), however, works the same way as the : operator does. For example, if you type seq(1, 10) this will become clear. That is, running this command will generate the same sequence as in the first example:
Evidently, we got the same output as using the : operator. If we have a look at the documentation we can see that there are number of arguments that we can work with:
As you can see in the image above (or in the documentation): the first two arguments of seq() are “from =” and “to =”. In R, we do not have to use the name of the arguments. That is, if we write out their values in the same order as written in the function it will produce the same results as using the names. It is worth noting, however, for more complex functions best practice is to use the names of the arguments. This will also make the code much clearer. For example, we can generate a sequence of descending numbers like this:
In the next subsection, we will have a look at the “by=” argument that enables us to define the increment of the sequence.
In some cases, we may want, instead of 1 to 20, a vector of numbers ranging from 0 to 20, sequences incremented by e.g. 2. Here’s how to create a sequence of numbers with a specified increment step:
As you can see, in the image below, this produces a vector with fewer numbers but every number is increased by 2. In the next section, we will have a look at how to specify how many numbers we want to generate between two specified numbers.
Here’s how we can use the length argument to generate 30 numbers between 1 and 30:
Now, this generated 30 floating-point numbers between 1 and 30. If we want to check whether there really are 30 numbers in our vector we can use the length() function:
Now, as previously mentioned there are often many different approaches for solving the same problems. This is, of course, also for R statistical programming language. In general, choosing the simplest approach which includes as little code as possible is probably the way to go. That said, we will go to the next section where we will be learning to get a sequence of the same number (e.g, “0”). In a more recent post, you will learn 7 examples of when and how to use the %in% operator in R.
To get a repeated sequence of a number we can use the rep() function. Here’s how to create a vector containing 10 repetitions of the number 0:
Now, the rep() function can also be used together with the : operator, the c() or the seq() functions.
In this example, we are going to get the numbers 1, 2, 3 generated 10 times. Here’s how to repeat a sequence of numbers.
If we, on the other hand, want to replicate a sequence (e.g., 1 to 5) 10 times we can use the : operator:
Finally, it is also possible to get each number, that we want in our sequence, to be generated a specified amount of times:
Note, that if we want to repeat a function or generate e.g., sequences of numbers we can use the repeat and replicate functions in R. as well.
Check out the following posts if you need to extract elements from datetime in e.g. a vector:
Answer is posted for the following question.
Answer
SPECIAL REQUIREMENTS : Must be licensed to operate a motor vehicle in West Virginia Must be able to swim 37 ½ yards fully clothed Must pass periodic firearms
Answer is posted for the following question.
How to become dnr officer in wv?
Answer
The best way to choose your ideal mehndi design is to take hints from your personality Talk to your mehndi artist about your likes and dislikes
Answer is posted for the following question.
How to mehendi design?
Answer
All standard UK landline numbers start with 01 or 02, and all UK mobile numbers start with 07. Calls and texts to these numbers are included in your minutes and texts as part of your tariff. If you go over your allowance, you'll be charged at our standard UK rate.
Answer is posted for the following question.
What do o2 numbers start with?