Lecture 4: Commands and Calculations in R

Author

Dr. Logan Kelly

Published

September 4, 2024

Overview

  • In this lecture, we’ll explore:
    • How to type commands into the R console.
    • Performing basic calculations in R.
    • Storing numeric values in variables.
    • Using R as a calculator for arithmetic operations.

1. Typing Commands in the R Console

  • What is the R Console?
    • The Console Panel in RStudio is where you can directly type and execute R commands.
    • You can use the console to quickly run commands and view their output.
  • Example: Printing a message:
    • In the console, try typing the following command to print a message:
print("Hello, World!")
[1] "Hello, World!"

2. Doing Basic Calculations in R

  • Using R as a calculator:
    • R can perform basic arithmetic operations just like a calculator.
    • You can add, subtract, multiply, divide, and use exponents directly in the console.
  • Example: Simple arithmetic:
3 + 5  # Addition
[1] 8
10 - 4  # Subtraction
[1] 6
6 * 7  # Multiplication
[1] 42
8 / 2  # Division
[1] 4
2^3  # Exponentiation (2 raised to the power of 3)
[1] 8
  • Note: Comments in R can be written using #. Any text following # will be ignored by R when executing commands.

3. Storing Numbers as Variables

  • What is a variable in R?
    • A variable in R is a way to store a value or result that you can reuse later in your code.
  • Example: Assigning a value to a variable:
x <- 10  # Assign the value 10 to the variable x
y <- 5   # Assign the value 5 to the variable y
  • How to use variables:
    • Once a variable is assigned, you can use it in calculations or other commands.
  • Example: Using variables in calculations:
result <- x + y  # Add x and y, store the result in a new variable 'result'
result
[1] 15
  • Important notes:
    • In R, the assignment operator is <-, which assigns a value to a variable.
    • You can view the contents of any variable by typing its name in the console and hitting Enter.

4. Using Built-in Functions

  • What are functions in R?
    • Functions are predefined operations or procedures that take input, perform specific tasks, and return a result.
    • R includes a vast number of built-in functions for various tasks such as calculations, data manipulation, and statistical analysis.
  • Example: Calculating the square root:
sqrt(25)
[1] 5
  • Example: Finding the sum of a vector:
numbers <- c(1, 2, 3, 4, 5)  # Create a vector of numbers
sum(numbers)
[1] 15
  • Additional basic functions:
    • mean() – Calculates the average of a set of numbers.
    • length() – Returns the number of elements in a vector or list.
    • min() and max() – Returns the smallest or largest value from a vector.

Key Takeaways

  • You have learned how to use the R console to run basic commands and calculations.
  • R can perform a variety of arithmetic operations, and you can store results in variables for reuse.
  • Built-in functions, such as sqrt() and sum(), are powerful tools for performing calculations in R.

Looking Forward

  • In the next lecture, we’ll dive deeper into functions, exploring how to create your own custom functions and use them to solve more complex problems in R.