Topics

  • Comment
  • Basic Arithmetic
  • Variable
  • Data types
  • class() function
  • str() function

Comment

# symbol is used to start a comment. Any text following # on the same line will be ignored by the R interpreter. This is a convenient way to add explanations or notes to your code without affecting its execution.

Formatting and organizing code within the script and document by using multiple levels of headings (#, ##, ###, ####), you can create a hierarchical structure that visually outlines different sections of your code. This can make it easier to navigate and understand, especially for larger or more complex projects.

Examples:

# This is a comment, Calculate the sum of 2 and 2
2 + 2
## [1] 4
# This is a comment, Calculate the difference of 27 subtracted by 10
27 - 10
## [1] 17

Basic Arithmetic

R is a powerful tool for performing various mathematical calculations. It supports a wide range of arithmetic operations, including:

Arithmetic Operators

  • Multiplication: *
  • Division: /
  • Addition: +
  • Subtraction: -
  • Exponentiation: ^
  • Modulo: %%

Examples:

# Multiplication
# This is a comment, Calculate the product of 9 multiplied by 8
9 * 8
## [1] 72
# Division
# This is a comment, Calculate the quotient of 6 divided by 2
6 / 2
## [1] 3
# Addition
# This is a comment, Calculate the sum of 1 and 1
1 + 1
## [1] 2
# Subtraction
# This is a comment, Calculate the difference of 27 subtracted by 10
27 - 10
## [1] 17
# Exponential
# # This is a comment, Calculate 'yth power of x', so called 
# "The 5th power of 2 is 32"
2^5 # (2 * 2 * 2 * 2 * 2)
## [1] 32
# Modulo
# # This is a comment, Calculate the remainder after division
# "28 modulo 6 is 4"
28%%6 # 28 divided by 6 is equal to 24, and remainder 4 is the modulo
## [1] 4

Variables

Variable is a container that stores a value, variable can hold a variety of data types such as numbers, text (strings), logical values (TRUE or FALSE) and others.
<- assignment values using the assignment operator

Examples:

# Assign the value of 28 to x
x <- 28

# Print out the value of the variable x
x
## [1] 28
# Creating a variable named "age" and assigning the value of 38 to it
age <- 38

# Print out the value of the variable age using print built-in function
print(age)
## [1] 38
### examples ----
# Assign a value to the variables my_salary and your_salary
my_salary <- 5000
your_salary <- 4500

# Add these two variables together
our_salary <- my_salary + your_salary

# Create the variable our_salary and print out
print(our_salary)
## [1] 9500

Data Types

R, a versatile programming language for statistical computing and data analysis, recognizes several fundamental data types. Understanding these data types is crucial for effective data manipulation and analysis. Here’s a breakdown of the primary data types in R:

Examples:

## Numeric ----
# Real numbers: Numbers with decimal points.
x <- 3.14159
print(x)
## [1] 3.14159
## Integers ----
# Whole Numbers.
y <- 42
print(y)
## [1] 42
## Characters ----
# Strings: Sequences of characters enclosed in quotes.
text <- "Hello, world!"
print(text)
## [1] "Hello, world!"
## Logical ----
# Boolean values: TRUE or FALSE. Represents 1 is TRUE and 0 is FALSE
is_true <- TRUE
is_false <- FALSE

print(is_true)
## [1] TRUE
print(is_false)
## [1] FALSE

class() function

class() is a built-in function in R that is used to determine the data type of an object. It returns a character string specifying the class of the object.

Examples:

class(x)
## [1] "numeric"
class(y)
## [1] "numeric"
class(text)
## [1] "character"
class(is_true)
## [1] "logical"
class(is_false)
## [1] "logical"

str() function

The str() function in R is a valuable tool for quickly inspecting the structure of an R object. It provides a concise summary of the object’s type, dimensions, and a glimpse of its contents.

Examples:

str(x)
##  num 3.14
str(y)
##  num 42
str(text)
##  chr "Hello, world!"
str(is_true)
##  logi TRUE
str(is_false)
##  logi FALSE