R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Data Types in R

R, being a language primarily designed for statistics and data analysis, offers a wide variety of data types to handle different forms of data. Understanding these data types is fundamental to working with R efficiently. In this tutorial, we'll cover the main data types you'll encounter in R.

1. Basic Data Types

1.1. Numeric

This is the default computational data type in R. If you input a number into R, it will assume it's numeric unless you specify otherwise.

x <- 23.5
class(x)  # Output: "numeric"

1.2. Integer

To specify an integer, use the L suffix.

y <- 23L
class(y)  # Output: "integer"

1.3. Complex

R can handle complex numbers with an imaginary component.

z <- 3 + 4i
class(z)  # Output: "complex"

1.4. Logical

Boolean values (TRUE/FALSE).

a <- TRUE
b <- (4 > 5)  # This will be FALSE
class(a)  # Output: "logical"

1.5. Character

Text/string values.

name <- "Alice"
class(name)  # Output: "character"

2. Data Structures

2.1. Vectors

A one-dimensional array that can hold numeric, integer, complex, logical, or character values. Elements in a vector must be of the same type.

vec <- c(1, 2, 3, 4, 5)
class(vec)  # Output: "numeric"

2.2. Matrices

Two-dimensional arrays where every element is of the same type.

mat <- matrix(1:6, nrow=2, ncol=3)
class(mat)  # Output: "matrix"

2.3. Arrays

Like matrices but can have more than two dimensions.

arr <- array(1:12, dim=c(2,3,2))
class(arr)  # Output: "array"

2.4. Factors

Vectors used to represent categorical data.

gender <- factor(c("Male", "Female", "Male"))
class(gender)  # Output: "factor"

2.5. Data Frames

Tabular data structure with columns that can be of different types.

df <- data.frame(Name=c("Alice", "Bob"), Age=c(25, 30))
class(df)  # Output: "data.frame"

2.6. Lists

Collections of objects, where each object can be of a different type.

lst <- list(Name="Alice", Age=25, Scores=c(90, 85, 92))
class(lst)  # Output: "list"

3. Checking and Coercing Data Types

3.1. Checking Data Types

You can use is. functions to check data types, e.g., is.numeric(), is.character(), is.matrix(), etc.

3.2. Coercing Data Types

You can convert one data type to another using as. functions:

num <- 5
as.character(num)

However, be cautious when coercing, as it can lead to data loss or unexpected results.

Conclusion

R provides a rich set of data types and structures to handle a wide range of data analysis tasks. Familiarizing yourself with these data types will help you use R more efficiently and avoid common pitfalls. As you progress, you'll learn about even more advanced data structures and types provided by various R packages.

  1. Numeric and integer data types in R:

    # Numeric data type
    num_var <- 3.14
    
    # Integer data type
    int_var <- 42L
    
  2. Character and string data types in R programming:

    # Character data type
    char_var <- "Hello, World!"
    
    # String data type
    str_var <- 'R Programming'
    
  3. Logical and boolean data types in R:

    # Logical data type
    logical_var <- TRUE
    
    # Boolean data type
    bool_var <- FALSE
    
  4. Handling date and time data types in R:

    # Date data type
    date_var <- as.Date("2022-01-01")
    
    # POSIXct for date and time
    datetime_var <- as.POSIXct("2022-01-01 12:30:00")
    
  5. Factor data type in R explained:

    # Creating a factor
    gender <- c("Male", "Female", "Male", "Female")
    factor_gender <- factor(gender)
    
    # Viewing levels of a factor
    levels(factor_gender)
    
  6. Working with lists and vectors in R:

    # Creating a vector
    my_vector <- c(1, 2, 3, 4, 5)
    
    # Creating a list
    my_list <- list("John", 25, TRUE, c(1, 2, 3))
    
  7. Conversion between data types in R:

    # Numeric to character conversion
    num_to_char <- as.character(num_var)
    
    # Character to numeric conversion
    char_to_num <- as.numeric(char_var)
    
    # Integer to numeric conversion
    int_to_num <- as.numeric(int_var)
    
    # Date to character conversion
    date_to_char <- as.character(date_var)
    
    # Character to date conversion
    char_to_date <- as.Date(date_to_char)