💡 Learn from AI

Introduction to R Programming

Lesson 2: Basic Data Types in R

Basic data types in R are fundamental building blocks that underlie all R programming tasks. In this lesson, we will introduce four basic data types in R: numeric, character, logical, and factor.

Numeric data type represents any real or imaginary number. For example, 1, -3, 5.5, 0.0001, and 2+3i are all numeric data types in R. Character data type represents any text string enclosed in double or single quotes. For example, "Hello, World!", 'R is cool', and '123' are all character data types. Logical data type represents a binary value, TRUE or FALSE. Factor data type represents a categorical variable with a fixed set of possible values, also known as levels.

To create a variable with a numeric data type, we can use the assignment operator <- or =. For example, x <- 5 creates a variable x with value 5. To create a variable with a character data type, we can use quotes. For example, y <- 'hello' creates a variable y with value 'hello'. To create a variable with a logical data type, we can use the keywords TRUE or FALSE. For example, z <- TRUE creates a variable z with value TRUE. To create a variable with a factor data type, we can use the factor() function. For example, w <- factor(c('low', 'medium', 'high')) creates a variable w with three levels: low, medium, and high.

In summary, understanding basic data types in R is essential for any programming tasks in R. It is important to note that R is a dynamically typed language, meaning we don't need to specify the data type when creating a variable. Instead, R will infer the data type based on the value assigned to a variable. This flexibility makes R a powerful and versatile programming language.

For further reading, we recommend the following resources:

  • The R Inferno by Patrick Burns
  • R Programming for Data Science by Roger D. Peng
  • An Introduction to R by Phil Spector.
Take quiz (5 questions)

Previous unit

Lesson 1: Introduction to R Programming

Next unit

Lesson 3: Data Structures in R

All courses were automatically generated using OpenAI's GPT-3. Your feedback helps us improve as we cannot manually review every course. Thank you!