How to Make a Continue Vector in R
A vector is a collection of variables and in R it is the most basic data structure. In R, a vector is a set of elements with a similar kind of data. A vector can store data in logical, numeric, double, alphanumeric, complex, or raw formats. The elements that make up a vector are called components of that vector. Vectors in R are similar to arrays in C, which contain numerous data items of the same kind. One important distinction is that vector indexing begins with '1' rather than '0' in R programming. To find out what type of vector we have, we can apply the "type()" method.
Atomic vectors and lists are the two types of vectors that exist. The key difference between both types is that in atomic vectors we have vectors of the same type while in a list we can use vectors of a different type.
By understanding and implementing the different ways and examples below, you will gain a sound knowledge of the creation of vectors in R.
Creating vector in R:
R provides various techniques and methods to create a vector. We will discuss 4 different methods for the creation of vectors with examples implemented in Rstudio in Ubuntu 20.04. Check that all of the methods and functions listed below are case-sensitive. Now, we'll go through each technique and show you how to utilize it in R script.
Vector Creation using the colon "(:)" Operator in R:
When you need to make a vector of numbers that are all the same size, the "colon(:)" operator can be the best option. To make a vector with continuous values, we may use the "(:)" operator.
Example:
In Rstudio we will write a script to create a vector by using the "(:)" operator. We will use the variable "v" and assign it a value. The "<-" operator is the basic operator for assigning value to a vector. After this "<-" operator, we'll write the range of values using the "(:)" operator.
"Print()" is the built-in command in R to display the output of an argument within its braces.
We will run this Rscript in our Ubuntu terminal. For this purpose, we'll first change the directory to where our Rscript file is saved. In this case, we have saved it in Desktop. We changed the directory from home to Desktop using the command "cd" writing the name of the desired directory next to it and pressing "Enter". This will land you in the Desktop folder.
The command used to run the Rscript file in the terminal is "Rscript". We'll write the "Rscript" command followed by the file name saved on Desktop with the ".R" extension. Now, press Enter and this will display the output of the code saved in Rscript.
Vector Creation using the "c()" function in Rstudio:
The "c()" function is a built-in function that combines its parameters. In R, the "c()" function helps to build a vector with user-defined values. The "c()" function concatenates several values together to form a vector or list.
Example:
We have used the "c()" function in the practical implementation below. We created a vector named "odd". Using the "<-" operator, assign the vector a list of numerical values. A comma is required between each value. In the print, the statement writes the name of the vector.
In the terminal of Ubuntu, run the file with the ".R" extension and press enter to see the output.
We can also use the "c()" function to create a character string. We created a vector named "animals" and assigned it 4 values. Each is separated with a comma. Keep in mind that while using a character data type, you must have to place quotation marks ("") around each value.
The result of the above-mentioned Rscript may be seen here.
Creating Vector using the "assign()" function in R:
The "assign()" function in the R programming is also used to create a vector for the numeric values. The new vector's name must be specified within the "assign()" function as well as the values we'd want to save inside this vector object.
Example:
This example will show you storing numeric values in a vector. We will use the "assign()" function inside the braces of the function. We will write the vector name in which we want to store the numeric value. With a ":" operator, we will define the range of the values that has to be saved. The "Print()" statement will simply display the output.
The output of the above script shows values of the numeric vector from 13 to 17 in a list.
Creating Vector using "Seq()" Function in R :
Another method to create a vector in R programming is by using the "seq()" function. It is used to create a vector element sequence. There are two methods to utilize the "seq()" function. It helps in specifying the number of instances in an interval with the "by" parameter and the size of the steps with the "length.out" parameter.
Example:
In the given example, we have created 2 vectors, "v1" and "v2″, with the utilization of the "seq()" function. By the syntax of the "seq()" function, inside the braces of the function we have defined the value from which the vectors start (i.e., 10) and separated by a comma. The value to which the vector must end which is 30 in our example. Whereas, "by" is the difference between defined vectors which we have set to 3. The "length.out" is the vector's highest length which we defined as 7.
We have displayed the output of both the vectors "v1" and "v2" in our Ubuntu terminal by using the "Rscript" command.
Conclusion:
Creating vectors in R is fundamental lesson for programmers. In this of writing, we have tried to make you familiar with different ways of creating vectors in Rscript. We have demonstrated the utilization of various functions to create vectors, each with very simple and easy-to-understand practical examples. We implemented all the example codes in Rstudio in Ubuntu 20.04.
Source: https://linuxhint.com/create-vector-r/
0 Response to "How to Make a Continue Vector in R"
Postar um comentário