# Data Types in R

There are different types of data sets that can be used in R. These data sets include integer, numeric, logical, character, complex and raw data sets. The integer data, as the name says, represent the integer numbers without any decimal points. These types of data in R is used to store concrete or countable values. Numeric data, on the other hand, stores all kind of data whether it’s fractions, negative numbers, whole number or integers. It represents all kind of quantitative values.

Logical data consists of binary values or conditions that are essential for certain operations. For example, FALSE and TRUE values fall under logical data.

Character data is used to represent texts such as words and sentences. These strings are created using single or double quotes in R.

Complex numbers or complex data consists of complex numbers, including mathematical and scientific computations. Raw data stores unprocessed data.

Starting with the integer data, we can create the integer data by using the following command in R

`int_data <- 15L`

The L is used to explicitly indicate that the value should be treated as an integer rather than a regular numeric value. In R, you can use the L suffix to indicate that a number should be saved as an integer.

If you want to know what kind of data is this, use the following command

`class(int_data)`

The console window shows that it is an integer data.

Similarly, if you want to create the numeric data, then use the following command

`num_data <- 19.2`

The value 19.2 is stored in the vector named num_data. Like previous action, if you want to know the type of data, use the following command

`class(num_data)`

The console window shows that it is a numeric data, having 19.2 as a number.

If you want to check the type of the value stored in the num_data variable, you can use the following command

`typeof(num_data)`

The typeof() function is used in R to determine type of the value stored in a vector, not the class of the vector. In this case, type of value is double, as shown below. This is because decimal points in R are usually represented with double-digit precision.

Similarly, for the logical data, if we want to check whether a certain value is TRUE or FASLSE, we can do that too in R. For instance, if we want to check whether the value in the integer, we created earlier, is greater than 13 or not. For that, we use the following command

`check <- int_data > 13`

Running this command gives the following result in environment window

As you can see in the above image, the logical data says that it is TRUE that integer has value greater than 13. Remember that, we created the integer value equal to 15.

To check the type of data, use the following command

It says that data is logical.

Moving on, sometimes we have a non-numeric data that can contain texts or event sentences. This type of data is known as character data. To give you an example of character data, let’s create the following data

`name <- "TheDataHall"`

This will create the vector by the name having value TheDataHall. The same value can be created using the single quotes, as shown in the command below

`name <- ‘TheDataHall’`

If we want to find out the class type of the data, we use the following command

`class(name)`

The above command will give following results for both single and double quotes operations.

## Check the data types of Object in R :

We can also check the data types of object or the data created earlier in R, through the logical format. For instance, if we run the following command

`check_int <- is.integer(int_data)`

The above command will check whether the value stored in the int_data variable is of integer type or not. After running this code, the check_int variable will provide either TRUE or FALSE in results depending on whether the data is integer or not. As in this case, the data is integer, the check_int has a value of TRUE, in the environment window, as shown below

Similarly, if we run the same command for num_data created earlier, the result will now be FALSE, because the data is not integers, but is numeric. The command will be as following

`check_int2 <- is.integer(num_data)`

Similarly, if you want to check whether the variable `num_data` contains numeric values or not. Running the following command will return TRUE, as the data is numeric.

check_num <- is.numeric(num_data)

This is shown as below

Similarly, if you want to check whether int_data contains numeric values or not. To do so, run the following command,

`check_num <- is.numeric(int_data)`

The result of the above command are as following, showing the result as TRUE because int_data does contain numeric values.

Similarly, if we wish to proceed on checking the logic of name data, and find out whether it is numeric or not, we use the following command

`check_num <- is.numeric(name)`

As we already know, the name data has text in it, and it is non-numeric data, so the return we will get from above command is FALSE. This is also visualized below

Subscribe
Notify of