Welcome to the second lesson of the R Programming course! In this lesson, we will cover the basic syntax of R, which is essential for writing and understanding R code. By the end of this lesson, you will be familiar with the fundamental building blocks of R programming.

  1. Comments

Comments are used to explain the code and make it more readable. In R, comments start with the # symbol.

# This is a single-line comment
x <- 10  # This is an inline comment

  1. Variables and Assignment

Variables are used to store data. In R, the assignment operator <- is commonly used to assign values to variables.

# Assigning a value to a variable
x <- 5
y <- "Hello, R!"

# Printing the values of variables
print(x)
print(y)

Exercise 1: Variable Assignment

Assign the value 20 to a variable named a and the value "R Programming" to a variable named b. Print both variables.

# Your code here

Solution:

a <- 20
b <- "R Programming"
print(a)
print(b)

  1. Data Types

R supports various data types, including numeric, character, logical, and more.

  • Numeric: Represents numbers.
  • Character: Represents text strings.
  • Logical: Represents TRUE or FALSE.
# Numeric
num <- 42.5

# Character
char <- "Data Science"

# Logical
logi <- TRUE

Exercise 2: Data Types

Create a numeric variable num_var with the value 100, a character variable char_var with the value "Learning R", and a logical variable logi_var with the value FALSE. Print all three variables.

# Your code here

Solution:

num_var <- 100
char_var <- "Learning R"
logi_var <- FALSE
print(num_var)
print(char_var)
print(logi_var)

  1. Basic Operations

R can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

# Addition
sum <- 5 + 3

# Subtraction
diff <- 10 - 4

# Multiplication
prod <- 6 * 7

# Division
quot <- 20 / 4

# Printing the results
print(sum)
print(diff)
print(prod)
print(quot)

Exercise 3: Basic Operations

Calculate the sum of 15 and 25, the difference between 50 and 30, the product of 8 and 9, and the quotient of 100 divided by 5. Print all results.

# Your code here

Solution:

sum_result <- 15 + 25
diff_result <- 50 - 30
prod_result <- 8 * 9
quot_result <- 100 / 5
print(sum_result)
print(diff_result)
print(prod_result)
print(quot_result)

  1. Functions

Functions are used to perform specific tasks. R has many built-in functions, and you can also create your own.

Built-in Functions

# Using the sqrt() function to calculate the square root
sqrt_val <- sqrt(16)

# Using the paste() function to concatenate strings
greeting <- paste("Hello", "World")

# Printing the results
print(sqrt_val)
print(greeting)

Creating Your Own Function

# Defining a function to add two numbers
add_numbers <- function(a, b) {
  result <- a + b
  return(result)
}

# Using the function
sum_result <- add_numbers(10, 20)
print(sum_result)

Exercise 4: Functions

Create a function named multiply_numbers that takes two arguments and returns their product. Use this function to multiply 7 and 8, and print the result.

# Your code here

Solution:

multiply_numbers <- function(a, b) {
  result <- a * b
  return(result)
}

product_result <- multiply_numbers(7, 8)
print(product_result)

Conclusion

In this lesson, we covered the basic syntax of R, including comments, variables, data types, basic operations, and functions. These fundamental concepts are essential for writing and understanding R code. In the next lesson, we will delve into data types and structures in more detail.

Keep practicing the exercises to reinforce your understanding, and feel free to experiment with the code examples provided. Happy coding!

© Copyright 2024. All rights reserved