Introduction to R Programming - Day 1

Author

Dr. Mohammad Nasir Abdullah

Introduction to R Programming

This documentation is to introduce R programming for the first time.

1) Starting up - knowing the commenting in R console and script

We can make a comment for every codes that we write in R Script by typing ‘#’.

It is a good practice to write a comment at the end of the codes that we write. This is to prevent miss-understanding about the code in future. This can be done as follows:

# This is a comment
# Anything we type after '#' consider as a comment
1+1 #this is a proper way to write a comment after writing a line of code.
[1] 2
# comment will not be executed in R console
# this is another example that comment is not executed in R console: 
# 1 + 1

You can make all above as comment at once by selecting all codes and hit “ctrl + shift + c”.

2) Explaining output on console

print("Dr. Mohammad Nasir Abdullah")
[1] "Dr. Mohammad Nasir Abdullah"

You can type this code in the console and it will print the output in the same console. The code print(“Mohammad Nasir Abdullah”) is the code chunk and the result [1] “Mohammad Nasir Abdullah” is the output produced by R.

3) Object assignation

You can create variables from within the R environment and from files on your computer. R uses “=” or “<-” to assign values to a variable name.

Example 1: Assign using “=”

x = 2
print(x)
[1] 2

Example 2: Assign using “<-”

y <- 2
print(y)
[1] 2

4) R is case sensitive

One of the fundamental aspects to understand while working with R is its case sensitivity. In R, the identifiers such as variable names, function names, and other object names are case-sensitive. This means that Variable, variable, and VaRiAbLe are considered different identifiers in R.

It is crucial to maintain consistent capitalization to ensure that your code works as expected.

Example 1:

# Case sensitivity in R
Variable <- 1
variable <- 2

#The following will output 1, not 2
print(Variable)
[1] 1
#The following will output 2, not 1
print(variable)
[1] 2

In this example, Variable and variable are treated as two distinct objects, each holding different values.

5) Listing the objects in the workspace

The operations from previous sections led to the creation of several simple R objects. These objects are stored in the current R workspace. A list of all objects in the current workspace can be printed to the screen using objects() function.

objects()
[1] "has_annotations" "variable"        "Variable"        "x"              
[5] "y"              

A synonym for objects() is ls(). Remember that if we quit our R session without saving the workspace image,then these objects will disappear. If we save the workspace image, then the workspace will be restored at out next R session.

Data Types in R

One of the fundamental concepts in any programming language is the data type. In R, there are several basic data types that are used to define the type of data that can be stored and manipulated.

Basic data types in R:

1) Numeric: represent both integer and decimal numbers.

x <- 23.5
y <- 4

2) Character: represents strings or text.

name <- "Mohammad Nasir Abdullah"
greeting <- "Hello, World!"

3) Logical: represents boolean values (“TRUE” or “FALSE”).

is_true <- TRUE
is_false <- FALSE

4) Complex: represents complex numbers.

z <- 3 + 4i

5) Raw: represents raw bytes

raw_data <- charToRaw("Hello")

Data Structures in R

While the basic data types define a single value, R offers several data structure to store collections of values:

1) Vectors: A one-dimensional array that can hold elements of the same data type.

numeric_vector <- c(1,2,3,4,5,6)
character_vector <- c("apple", "banana", "cherry")

2) Matrix: A two-dimensional array where elements are arranged in rows and columns

matrix_data <- matrix(1:6, nrow=2, ncol=3)

3) List: A collection that can hold elements of different data types.

my_list <- list(name="Nasir", age=16, score=c(100,99,100))

4) Data frame: A table-like structure where columns can be of different data types

students <- data.frame(Name=c("Ali", "Abu", "Ahmad"), 
                       Age = c(28, 39,10), 
                       Grade = c("A", "A","A+"))

5) Factor: Used to represent categorical data

gender <- factor(c("Male", "Female", "Male"))

Checking and converting data types

To check the data type or structure of a variable, use the class() function:

class(name)
[1] "character"

To convert between data types, use function like as.numeric(), as.character(), as.factor(), etc..

num <- as.numeric("123")

Exercise!

  1. What is the primary purpose of using comments in R codes?
  2. Write a line of R code and then comment it out.
  3. Explain the difference between single-line and multi-line comments in R.
  4. Comment the following code explaining what each line does;
x <- c(1,2,3,4,5)
mean(x)
  1. In R, what is the primary symbol used to assign values to objects?

  2. Assign the number 5, 10, and 15 to an object named my_numbers using a vector.

  3. what will be the output of the following code:

    a <- 5
    b <- 7
    a <- b
    print(a)
    [1] 7
  4. Explain the difference between “=” and “<-” in R, and state which one is recommended for assignment.

  5. What are the primary data types in R?

  6. Create a vector mix containing a number, a character, and a logical value.

  7. How do you check the data type of an object in R?

  8. What will be the output of the following code and why:

    is.numeric("5")
    [1] FALSE
  9. Explain the difference between a factor and a character data type in R.