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 comment1+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 =2print(x)
[1] 2
Example 2: Assign using “<-”
y <-2print(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 RVariable <-1variable <-2#The following will output 1, not 2print(Variable)
[1] 1
#The following will output 2, not 1print(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.5y <-4
2) Character: represents strings or text.
name <-"Mohammad Nasir Abdullah"greeting <-"Hello, World!"
3) Logical: represents boolean values (“TRUE” or “FALSE”).
is_true <-TRUEis_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.