recategorized

# R Data Types

## Basic data types

R Programming works with numerous data types, including

• Scalars
• Vectors (numerical, character, logical)
• Matrices
• Data frames
• Lists

Basics types

• 4.5 is a decimal value called numerics.
• 4 is a natural value called integers. Integers are also numerics.
• TRUE or FALSE is a Boolean value called logical.
• The value inside " " or ' ' are text (string). They are called characters.

We can check the type of a variable with the class function

Example 1:

```# Declare variables of different types
# Numeric
x <- 28
class(x)```

Output:

`##  "numeric"`

Example 2:

```# String
y <- "R is Fantastic"
class(y)```

Output:

`##  "character"`

Example 3:

```# Boolean
z <- TRUE
class(z)```

Output:

`##  "logical"`

## Variables

Variables store values and are an important component in programming, especially for a data scientist. A variable can store a number, an object, a statistical result, vector, dataset, a model prediction basically anything R outputs. We can use that variable later simply by calling the name of the variable.

To declare a variable, we need to assign a variable name. The name should not have space. We can use _ to connect to words.

To add a value to the variable, use <- or =.

Here is the syntax:

```# First way to declare a variable:  use the `<-`
name_of_variable <- value
# Second way to declare a variable:  use the `=`
name_of_variable = value```

In the command line, we can write the following codes to see what happens:

Example 1:

```# Print variable x
x <- 42
x```

Output:

`##  42`

## Vectors

A vector is a one-dimensional array. We can create a vector with all the basic data type we learnt before. The simplest way to build a vector in R, is to use the c command.

Example 1:

```# Numerical
vec_num <- c(1, 10, 49)
vec_num```

Output:

`##   1 10 49`