Basic Data Types in R and Type Conversion

 

Basic Data Types in R and Type Conversion

Introduction

Data types specify the kind of values that can be stored and manipulated in a program. Since R is a dynamically typed language, variables do not need explicit declarations; the type is determined automatically based on the assigned value.

Understanding data types and type conversion is essential because different operations require different types of data. R provides several built-in functions to identify and convert data types.


Basic Data Types in R

The fundamental (atomic) data types in R are:

  1. Numeric
  2. Integer
  3. Character
  4. Logical
  5. Complex
  6. Raw

1. Numeric Data Type

Numeric values represent decimal numbers (double precision).

Example

x <- 25.6
y <- 100

print(x)
print(y)

Output

[1] 25.6
[1] 100

Characteristics

  • Used for real numbers.
  • Default type for numbers in R.
  • Supports arithmetic operations.

2. Integer Data Type

Integers are whole numbers. An integer is created by appending L to the number.

Example

x <- 25L
y <- 100L

print(x)
print(y)

Output

[1] 25
[1] 100

Characteristics

  • Stores whole numbers.
  • More memory efficient than numeric values.

3. Character Data Type

Character values are strings enclosed within quotes.

Example

name <- "John"
city <- "Delhi"

print(name)
print(city)

Output

[1] "John"
[1] "Delhi"

Characteristics

  • Used to store text.
  • Strings may contain letters, digits, and symbols.

4. Logical Data Type

Logical values represent Boolean values.

Example

x <- TRUE
y <- FALSE

print(x)
print(y)

Output

[1] TRUE
[1] FALSE

Characteristics

  • Used in decision-making and comparisons.
  • Possible values are TRUE and FALSE.

5. Complex Data Type

Complex numbers contain a real and an imaginary part.

Example

z <- 3 + 4i

print(z)

Output

[1] 3+4i

Characteristics

  • Used in scientific and engineering computations.

6. Raw Data Type

Stores raw bytes.

Example

x <- charToRaw("ABC")

print(x)

Output

[1] 41 42 43

Characteristics

  • Used for binary data manipulation.

Determining the Data Type

R provides several functions to determine the type of a variable.


1. class()

Returns the class of an object.

Syntax

class(object)

Example

x <- 10
y <- "Hello"
z <- TRUE

class(x)
class(y)
class(z)

Output

[1] "numeric"
[1] "character"
[1] "logical"

2. typeof()

Returns the internal storage type.

Example

x <- 10
y <- 10L
z <- "R"

typeof(x)
typeof(y)
typeof(z)

Output

[1] "double"
[1] "integer"
[1] "character"

3. mode()

Returns the mode of storage.

Example

x <- 20

mode(x)

Output

[1] "numeric"

4. str()

Displays the structure of an object.

Example

x <- c(10,20,30)

str(x)

Output

num [1:3] 10 20 30

Type Checking Functions

R provides functions to check whether an object belongs to a particular type.

FunctionPurpose
is.numeric()    Checks numeric values
is.integer()    Checks integer values
is.character()    Checks character values
is.logical()    Checks logical values
is.complex()    Checks complex values

Example

x <- 25

is.numeric(x)
is.character(x)

Output

[1] TRUE
[1] FALSE

Type Conversion in R

Type conversion means converting one data type into another.

R provides several conversion functions:

Function    Converts To
as.numeric()    Numeric
as.integer()    Integer
as.character()    Character
as.logical()    Logical
as.complex()        Complex

Converting Character to Numeric

Example

x <- "100"

y <- as.numeric(x)

print(y)

Output

[1] 100

Verify Type

class(y)

Output:

[1] "numeric"

Converting Numeric to Character

Example

x <- 50

y <- as.character(x)

print(y)

Output

[1] "50"

Verify Type

class(y)

Output

[1] "character"

Converting Numeric to Integer

Example

x <- 45.8

y <- as.integer(x)

print(y)

Output

[1] 45

Note: Decimal part is truncated.


Converting Character to Integer

Example

x <- "150"

y <- as.integer(x)

print(y)

Output

[1] 150

Converting Numeric to Logical

Example

x <- 1

as.logical(x)

Output

[1] TRUE

x <- 0

as.logical(x)

Output

[1] FALSE

Converting Character to Logical

Example

x <- "TRUE"

y <- as.logical(x)

print(y)

Output

[1] TRUE

Converting Numeric to Complex

Example

x <- 25

z <- as.complex(x)

print(z)

Output

[1] 25+0i

Implicit Type Conversion

R automatically converts data types when necessary.

Example 1

x <- c(10,20,30,"R")
print(x)

Output

[1] "10" "20" "30" "R"

Since one element is character, all elements become characters.


Example 2

x <- c(TRUE, FALSE, 10)

print(x)

Output

[1] 1 0 10

Logical values are converted into numeric values.


Hierarchy of Type Coercion in R

logical

integer

numeric

complex

character

During automatic conversion, lower types are converted to higher types.


Complete Example

# Variables
a <- 25
b <- "100"
c <- TRUE

# Check type
class(a)
class(b)
class(c)

# Convert character to numeric
num <- as.numeric(b)

# Convert numeric to character
str1 <- as.character(a)

# Convert logical to numeric
value <- as.numeric(c)

# Display results
cat("num =", num, "\n")
cat("str1 =", str1, "\n")
cat("value =", value)

Output

num = 100
str1 = 25
value = 1

Summary of Type Identification Functions

FunctionPurpose
class()    Returns class
typeof()    Returns internal type
mode()    Returns storage mode
str()    Displays structure
is.numeric()    Checks numeric type
is.integer()    Checks integer type
is.character()    Checks character type
is.logical()    Checks logical type

Summary of Conversion Functions

FunctionDescription
as.numeric()    Converts to numeric
as.integer()    Converts to integer
as.character()    Converts to character
as.logical()    Converts to logical
as.complex()    Converts to complex

Conclusion

R supports several built-in data types such as numeric, integer, character, logical, complex, and raw. Functions like class(), typeof(), mode(), and str() help determine the type of variables, while functions such as as.numeric(), as.integer(), as.character(), as.logical(), and as.complex() allow explicit type conversion. Understanding these concepts is fundamental for effective programming and data analysis in R

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Sample Programs to Explore Basic Concepts in R

Basic Input and Output Functions in R