R Data types

A variable in R stores data of different types, like numeric, string, float, etc. An R variable gets created when a value is assigned i.e. mentioning any specific type name is not required.

The following are the datatypes in R:

  • numeric
  • integer
  • complex
  • character
  • logical/ boolean
  • vectors
  • lists

Let us understand the datatypes one by one with examples:

Numeric data type in R

The numeric type stores numbers, decimal numbers, etc. Let us see an example:

# numbers
# an integer gets declared by letter L
marks <- 99
points <- 125.50

# Display
marks
points

# Display the class of the data type
class(marks)
class(points)

Output

[1] 99
[1] 125.5
[1] "numeric"
[1] "numeric"

Integer data type in R

The integer type stores integers and an appended L is used to declare an integer in R. Let us see an example:

# integer
# An integer gets declared by letter L
a <- 5L

# Display
a

# Display the class of the data type
class(a)

Output

[1] 5
[1] "integer"

Complex data type in R

The complex type stores complex numbers. It consists of the Real and Imaginary parts with i being imaginary. Let us see an example:

# Complex
# i is the imaginary part
a <- 5 + 7i

# Display
a

# Display the class of the data type
class(a)

Output

[1] 5+7i
[1] "complex"

Character data type in R

The character/ string type stores characters and strings. Use a single quote or double quote while creating a character or string type. Let us see an example:

# string
name_one <- "Amit"

# character
name_two <- "K"

# Display the names
name_one
name_two

# Display the class of the data type
class(name_one)
class(name_two)

Output

[1] "Amit"
[1] "K"
[1] "character"
[1] "character"

Logical/ boolean data type in R

The boolean type has two values, TRUE or FALSE. Let us see an example:

# Student Result
passed <- TRUE

# Display the result
passed

# Display the class of the data type
class(passed)

Output

[1] TRUE
[1] "logical"

Vectors in R

The vectors are elements of the same time, separated by a comma. The c() function combines such elements and forms a vector i.e. to create a Vector, use the c() in R programming. Let us see an example:

# Student Names
names <- c("amit", "john", "rohit", "tom")

# Display the names
names

# Display the class of the data type
class(names)

Output

[1] "amit"  "john"  "rohit" "tom"  
[1] "character"

Lists in R

If you want to add elements of different data types, then use the lists in R. The list() function is used in R to create a List. Let us see an example:

# Create the List
my_list <- list("amit", c(5, 10), 100, 20.95, "john")

# Display the List
my_list

# Display the class of the data type
class(my_list)

Output

[[1]]
[1] "amit"

[[2]]
[1]  5 10

[[3]]
[1] 100

[[4]]
[1] 20.95

[[5]]
[1] "john"

[1] "list"

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

R Variables
R Loops
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment