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

Variables in R

In R, a variable provides a way of naming and storing data for later use. They can store a variety of data types, including numbers, strings, vectors, and more. This tutorial will provide an overview of how to use variables in R.

1. Creating Variables

1.1 Basic Assignment

In R, the <- symbol is often used for assignment, though = can also be used.

x <- 5  # Assigning the value 5 to x
name <- "Alice"  # Assigning the string "Alice" to name

1.2 Multiple Assignments

You can assign multiple values to multiple variables at once.

x <- y <- z <- 10  # Assigning 10 to x, y, and z

2. Using Variables

Once a variable has been assigned, you can use it just like you would use the value itself.

x <- 5
y <- 10
sum <- x + y  # Adding the values of x and y, and storing in sum
print(sum)  # Outputs: 15

3. Variable Types

Variables in R can hold different types of data. Here are some of the common ones:

3.1 Numeric

Numbers, including integers and decimals.

num1 <- 5
num2 <- 5.5

3.2 Character

Strings of text.

str1 <- "Hello"
str2 <- 'World'

3.3 Logical

Boolean values: TRUE or FALSE.

bool1 <- TRUE
bool2 <- (5 > 10)  # This will evaluate to FALSE

3.4 Vectors

Ordered collections of values. All values in a vector must be of the same type.

vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c("a", "b", "c")

3.5 Factors, Lists, and More

There are many other advanced types like factors, lists, data frames, and matrices, which you'll encounter as you dive deeper into R.

4. Variable Naming Conventions

There are some best practices when naming variables in R:

  • Variable names should be descriptive and not too long.
  • Use underscores to separate words: user_age, first_name.
  • Avoid using names of existing functions and variables: don't use c, mean, etc.
  • Variable names in R are case sensitive: result, Result, and RESULT are three different variables.

5. Checking and Removing Variables

5.1 Checking Variables

You can see all the variables in your workspace using the ls() function.

ls()

5.2 Removing Variables

To remove a variable, use the rm() function.

x <- 5
rm(x)  # This will remove the variable x

Conclusion

Variables are the building blocks of any programming language, including R. Properly naming, assigning, and managing your variables is crucial for writing clear and efficient R scripts. As you get more advanced, you'll also encounter more complex data structures and types, expanding the ways you can store and manipulate data.

  1. Creating and assigning variables in R:

    • Assign values to variables using the assignment operator <- or =.
    # Example of creating and assigning variables
    age <- 25
    name <- "John"
    
  2. Data types of variables in R programming:

    • R supports various data types, including numeric, character, logical, and more.
    # Example of data types in R variables
    numeric_var <- 42
    character_var <- "Hello, R!"
    logical_var <- TRUE
    
  3. Variable naming conventions in R:

    • Follow conventions like using lowercase letters and avoiding reserved words.
    # Example of variable naming conventions
    user_name <- "Alice"
    
  4. Scope and visibility of variables in R:

    • Variables can have global or local scope, affecting their visibility.
    # Example of global and local variables
    global_var <- 10
    
    my_function <- function() {
      local_var <- 5
    }
    
  5. Global and local variables in R:

    • Global variables are defined outside functions, while local variables are defined within functions.
    # Example of global and local variables
    global_var <- 10
    
    my_function <- function() {
      local_var <- 5
    }
    
  6. Checking and changing variable types in R:

    • Use functions like class() to check variable types, and as.numeric() to change types.
    # Example of checking and changing variable types
    age <- "25"
    class_age <- class(age)
    numeric_age <- as.numeric(age)
    
  7. Missing values and variables in R:

    • Represent missing values using NA and handle them using functions like is.na().
    # Example of missing values in variables
    height <- c(160, NA, 175, 168)
    missing_values <- is.na(height)
    
  8. R code examples for variable manipulation:

    • Perform various manipulations on variables, such as arithmetic operations or string concatenation.
    # Example of variable manipulation
    x <- 5
    y <- 10
    sum_result <- x + y