# 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
This documentation is to introduce R programming for the first time.
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”.
print("Mohammad Nasir Abdullah")
[1] "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.
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.
= 2
x print(x)
[1] 2
<- 2
y print(y)
[1] 2
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.
# Case sensitivity in R
<- 1
Variable <- 2
variable
#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.
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] "variable" "Variable" "x" "y"
or
ls()
[1] "variable" "Variable" "x" "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.
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.
<- 23.5
x <- 4 y
2) Character
: represents strings or text.
<- "Mohammad Nasir Abdullah"
name <- "Hello, World!" greeting
3) Logical
: represents boolean values (“TRUE” or “FALSE”).
<- TRUE
is_true <- FALSE is_false
4) Complex
: represents complex numbers.
<- 3 + 4i z
5) Raw
: represents raw bytes
<- charToRaw("Hello") raw_data
R can do all mathematical operations:
In General….
1+1 # "+" means addition
2-1 # "-" means substract
2 * 3 # "*" means multiply
16/4 # "/" means devide
2 ^ 2 # "^" means exponentation
sqrt(16) #"sqrt" means square root of 2
log(10) # "log" means natural log
abs(-17) # "abs" means abolute value
15%%4 # "%%" means modulus (remainder)
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.
<- c(1,2,3,4,5,6)
numeric_vector <- c("apple", "banana", "cherry") character_vector
2) Matrix: A two-dimensional array where elements are arranged in rows and columns
<- matrix(1:6, nrow=2, ncol=3) matrix_data
3) List: A collection that can hold elements of different data types.
<- list(name="Nasir", age=16, score=c(100,99,100)) my_list
4) Data frame: A table-like structure where columns can be of different data types
<- data.frame(Name=c("Ali", "Abu", "Ahmad"),
students Age = c(28, 39,10),
Grade = c("A", "A","A+"))
5) Factor: Used to represent categorical data
<- factor(c("Male", "Female", "Male")) gender
To check the data type or structure of a variable, use class()
function:
class(name)
[1] "character"
To convert between data types, use function like as.numeric()
, as.character()
, as.factor()
, etc..
<- as.numeric("123") num
Comment the following code explaining what each line does;
<- c(1,2,3,4,5)
x mean(x)
Assign the number 5, 10, and 15 to an object named my_numbers
using a vector.
What will be the output of the following code:
<- 5
a <- 7
b <- b
a print(a)
Create a vector mix
containing a number, a character, and a logical value.
What will be the output of the following code and why:
is.numeric("5")