▼ Show 1 Answer

0 votes

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


## [1] "numeric"

Example 2:

# String
y <- "R is Fantastic"


## [1] "character"

Example 3:

# Boolean
z <- TRUE


## [1] "logical"


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


## [1] 42


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)


## [1]  1 10 49
Learn More with Madanswer

Related questions

0 votes
asked Nov 4, 2019 in R Language by MBarbieri