R Basics – Storing data

To meet the needs expressed by our readers in the survey, we start a short introductory course in R. We start our adventure by learning how to store data (data frames) in this language. For those who are not yet familiar with the environment in which we will be working, I refer you to the entries dealing with installation and first steps in R.

The first way to store data is as a vector. In a vector we can store data of only one type, for example numeric. As an example, let’s create a vector w that contains four numbers:

Display the created vector w in the console:

If we want to display a specific element of the vector, we need to type:

We can also display more elements:

Let us now create a vector containing consecutive numbers from 1 to 5:

The vector can also contain strings with, for example, five names:

What if we want to store different types of variables in one object? We can use the list object, which allows us to store different types of data in its elements. Let’s create a list that contains any three values of different types:

Display the created variable in the console:

To display the second element of the list, we need to type:

For two-dimensional data, such as grids, the values of each pixel are stored in a matrix. An arbitrary matrix is created with the matrix function by specifying the value with which it is to be filled and the number of rows and columns:

We can also insert any vector into the matrix whose number of elements is equal to the number of rows or all fields of the matrix:

Selecting any element in the matrix is done by specifying the row and column number:

An entire row or column can also be selected:

An matrix, just like a vector, can only store data of one type.

In R, you can also use multidimensional matrices called arrays. They are created by specifying the values stored in them and the dimension:

Another type of data storage is the factor. We will show how it works with an example. Let’s create a vector with towns names:

Let’s change this vector into a factor and plot it:

As we can see we have a vector and levels which are the names of the cities, to show how it works we convert the factor to a numeric vector:

As we can see, factor is a sequence of numbers that are assigned values from levels. When a value repeats, the same number appears in the string. In fact, how can we use it except for reducing the amount of memory needed to store this kind of data. For example, when counting the number of occurrences of a particular element

The last variable we consider, where we can store data, is the data frame. In the simplest way, it can be described as an Excel spreadsheet that stores data in columns and rows. In each column we can store a different type of data. Let’s create a simple data frame:

We have already dealt with this type of data in the analysis of vector files, because in this form the table is loaded with the attributes of the layer SHP. From the data frame, we can extract column as we did with the matrix:

or rows:

Each row and column also has a name that we can use to select:

We can show them too:

The choice of storage method depends on how the data is stored in the files. For example, as we mentioned earlier, grids will be an array and loaded spreadsheets will be a data frame. In the next stage of the course we will show how to use loops on the data.

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil