STATISTICS WITH R
DR. S. SUMATHI
ASST. PROFESSOR
R SYNTAX
To output text in R, use single or double quotes:
Example
"Hello World!"
To output numbers, just type the number (without quotes):
Example
5
10
25
To do simple calculations, add numbers together:
Example
5+5
R PRINT OUTPUT
Print
Unlike many other programming languages, you can output code in R without using a print function:
Example
"Hello World!“
R does have a print() function available
Example
print("Hello World!")
And there are times you must use the print() function to output code, for example when working with for
loops (which you will learn more about in a later chapter):
Example
for (x in 1:10) {
print(x)
It is up to you whether you want to use the print() function to output code. However, when your code is
inside an R expression (e.g. inside curly braces {} like in the example above), use the print() function to
output the result.
FUNDAMENTALS OF R
Variables:
R is a dynamically typed language, i.e. the variables are not declared with a data type rather they take the data type of the R-object
assigned to them. In R, the assignment can be denoted in three ways.
Using equal operator- data is copied from right to left.
variable_name = value
Using leftward operator- data is copied from right to left.
variable_name <- value
Using rightward operator- data is copied from left to right.
value -> variable_name
# R program to illustrate Initialization of
variables
# using equal to operator
var1 = "gfg"
print(var1) Output:
[1] "gfg"
# using leftward operator [1] "gfg"
var2 <- "gfg" [1] "gfg"
print(var2)
# using rightward operator
"gfg" -> var3
print(var3)
Comments:
Comments are the english sentences that are used to add useful information to the source code to make it more
understandable by the reader.
It explains the logic part used in the code and will have no impact in the code during its execution. Any statement starting with
“#” is a comment in R.
# all the lines starting with '#’ are comments and will be ignored during the execution of the program
# Assigning values to variables
a <- 1
b <- 2
# Printing sum
print(a + b)
OUTPUT
[1] 3
Concatenate Elements
For numbers, the + character works as a mathematical operator:
You can also concatenate, or join, two or more
Example
elements, by using the paste() function.
num1 <- 5
num2 <- 10
To combine both text and a variable, R uses comma (,): num1 + num2
If you try to combine a string (text) and a number, R will give you
Example an error:
text <- "awesome"
Example
paste("R is", text) num <- 5
text <- "Some text"
You can also use , to add a variable to another variable:
num + text
Example
Result:
text1 <- "R is"
Error in num + text : non-numeric argument to binary operator
text2 <- "awesome"
paste(text1, text2)
Operators
Operators are the symbols directing the various kinds of operations that can be performed between the operands.
Operators simulate the various mathematical, logical and decision operations performed on a set of Complex Numbers, Integers, and Numericals
as input operands.
These are classified based on their functionality –
Arithmetic Operators
Logical Operators
Relational Operators
Assginment Operators
Arithmetic Operators:
Arithmetic operations simulate various math operations, like addition, subtraction, multiplication, division and modulo.
# R program to illustrate the use of Arithmetic operators
a <- 12
b <- 5
# Performing operations on Operands
cat ("Addition :", a + b, "\n")
cat ("Subtraction :", a - b, "\n")
cat ("Multiplication :", a * b, "\n")
cat ("Division :", a / b, "\n")
cat ("Modulo :", a %% b, "\n")
cat ("Power operator :", a ^ b)
Logical Operators
Logical operations simulate element-wise decision operations, based on the specified operator between the
operands, which are then evaluated to either a True or False boolean value.
# R program to illustrate the use of Logical operators
vec1 <- c(FALSE, TRUE)
vec2 <- c(TRUE,FALSE)
# Performing operations on Operands
cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1 && vec2, "\n")
cat ("Logical OR :", vec1 || vec2, "\n")
cat ("Negation :", !vec1)
Logical Operators
Logical operations simulate element-wise decision operations, based on the specified operator between the operands, which
are then evaluated to either a True or False boolean value.
# R program to illustrate the use of Logical operators
vec1 <- c(FALSE, TRUE)
vec2 <- c(TRUE,FALSE)
# Performing operations on Operands
cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1 && vec2, "\n")
cat ("Logical OR :", vec1 || vec2, "\n")
cat ("Negation :", !vec1)
Relational Operators
The relational operators carry out comparison operations between the corresponding elements of the operands.
Example:
# the use of Relational operators
a <- 10
b <- 14
# Performing operations on Operands
cat ("a less than b :", a < b, "\n")
cat ("a less than equal to b :", a <= b, "\n")
cat ("a greater than b :", a > b, "\n")
cat ("a greater than equal to b :", a >= b, "\n")
cat ("a not equal to b :", a != b, "\n")
Assignment Operators
# Performing operations on Operands
Assignment operators are used to assign values to various data objects
cat("Value 1 :", v1, "\n")
in R.
cat("Value 2 :", v2, "\n")
Example:
cat("Value 3 :", v3, "\n")
# R program to illustrate the use of Assignment operators
cat("Value 4 :", v4, "\n")
cat("Value 5 :", v5)
# Left assignment operator
v1 <- “Welcome to R Programming"
v2 <<- " Welcome to R Programming "
v3 = " Welcome to R Programming "
# Right Assignment operator
" Welcome to R Programming " ->> v4
" Welcome to R Programming " -> v5
DATA TYPES
Data Types Example Description
Decimal values are called numerics in R. It
Numeric 1, 2, 12, 36
is the default data type for numbers in R.
R supports integer data types which are the
set of all integers. Capital ‘L’ notation as a
Integer 1L, 2L, 34L
suffix is used to denote that a particular
value is of the integer data type.
Logical TRUE, FALSE Take either a value of true or false
Set of all the complex numbers. The
Complex 2+3i, 5+7i complex data type is to store numbers with
an imaginary component.
R supports character data types where you
Character ‘a’, ’12’, “GFG”, ”’hello”’ have all the alphabets and special
characters.
# A simple R program to illustrate data type
print("Numberic type")
# Assign a decimal value to x
x = 12.25
# print the class name of variable
print(class(x))
# print the type of variable
print(typeof(x))
print("----------------------------")
print("Integer Type")
# Declare an integer by appending an
# L suffix.
y = 15L
# print the class name of y
print(class(y))
# print the type of y
print(typeof(y))
print("----------------------------")
print("Logical Type")
# Sample values
x=1
y=2
# Comparing two values
z=x>y
# print the logical value
print(z)
# print the class name of z
print(class(z))
# print the type of z
print(typeof(z))
print("----------------------------")
print("Complex Type")
# Assign a complex value to x
x = 12 + 13i
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
print("----------------------------")
print("Character Type")
# Assign a character value to char
char = "GFG"
# print the class name of char
print(class(char))
# print the type of char
print(typeof(char))
THANK YOU