📊

Understanding Data Types in R

Oct 3, 2024

Programming 101: Types of Data in R

Overview

  • Focus on five main types of data in R
  • Discuss changing data types for variables
  • Learn how to add levels to a factor

Major Types of Data

  1. Nominal Data (Character)

    • Example: Name (text data)
  2. Ordinal Data (Factor)

    • Example: Height (categorical data with order)
    • Levels need to be defined
  3. Integer

    • Example: Age (whole number)
  4. Numeric

    • Example: Weight (can be any number, including decimals)
  5. Logical

    • Represents true/false conditions

Changing Data Types

  • To check the structure of a data frame:
    • Use the command str(friends)
  • Changing height from character to factor:
    • Use friends$height <- as.factor(friends$height)
  • Changing age from numeric to integer:
    • Use friends$age <- as.integer(friends$age)

Understanding Factors

  • Factors represent categorical data with levels
  • By default, R orders factor levels alphabetically
  • To change factor levels, use:
    • friends$height <- factor(friends$height, levels = c('short', 'medium', 'tall'))

Creating Logical Variables

  • A logical variable can be created by comparing values:
    • Example:
      • old <- friends$age > 23
  • This results in a logical vector of true/false values

Conclusion

  • These five types of data are essential for data analysis in R
  • Other types of data exist (e.g., date/time) but are less commonly used
  • Subscribe for more content on R programming!