Practical 1- Basics of R
Practical 1- Basics of R
# Data Input
# R program to illustrate
# taking input from the user
# taking input using readline()
# this command will prompt you
# to input a desired value
var <- readline();
# convert the inputted value to integer
var = as.integer(var);
# print the value
print(var)
# R program to illustrate
# taking input from the user
# double input using scan()
d = scan(what = double())
# string input using 'scan()'
s = scan(what = " ")
# character input using 'scan()'
c = scan(what = character())
# print the inputted values
print(d) # double
print(s) # string
print(c) # character
# Arithmetic Operators
# Addition(+) operator
# c function - combines element together
a <- c (1, 0.1)
b <- c (2.33, 4)
print (a+b)
# Subtraction(-) operator
a <- 6
b <- 8.4
print (a-b)
# Multiplication(*) operator
A=5
Z=34
print (A*Z)
# Division(/) operator
a <- 1
b <- 0
print (a/b)
# Power(^) operator
list1 <- c(2, 3)
list2 <- c(2,4)
print(list1^list2)
# Modulo Division(%%) operator
list1<- c(2, 3)
list2<-c(2,4)
print(list1%%list2)
# R program to illustrate
# the use of Arithmetic operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)
# Performing operations on Operands
# cat function is used to print out the screen or to a file
cat ("Addition of vectors :", vec1 + vec2, "\n")
cat ("Subtraction of vectors :", vec1 - vec2, "\n")
cat ("Multiplication of vectors :", vec1 * vec2, "\n")
cat ("Division of vectors :", vec1 / vec2, "\n")
cat ("Modulo of vectors :", vec1 %% vec2, "\n")
cat ("Power operator :", vec1 ^ vec2)
# Vector Operations
# indexing starts from 1
# Creating a vector
# Use of 'c' function
# to combine the values as a vector.
# by default the type will be double
X <- c(1, 4, 5, 2, 6, 7)
print('using c function')
print(X)
# using the seq() function to generate
# a sequence of continuous values
# with different step-size and length.
# length.out defines the length of vector.
Y <- seq(0, 10, length.out = 5)
print('using seq() function')
print(Y)
# using ':' operator to create
# a vector of continuous values.
Z <- 5:10
print('using colon')
print(Z)
# Accessing an element in vector
# Accessing elements using the position number.
X <- c(2, 8, 1, 2, 5)
print('using Subscript operator')
print(X[4])
# Accessing specific values by passing
# a vector inside another vector.
Y <- c(4, 5, 2, 1, 7)
print('using c function')
print(Y[c(4, 1)])
# Logical indexing
Z <- c(5, 2, 1, 4, 4, 3)
print('Logical indexing')
Z[Z<4]
# Modifying a vector
# Creating a vector
X <- c(2, 5, 1, 7, 8, 2)
# modify a specific element
X[3] <- 11
print('Using subscript operator')
print(X)
# Modify using different logics.
X[X>4] <- 0
print('Logical indexing')
print(X)
# Modify by specifying the position or elements.
X <- X[c(5, 2, 1)]
print('using c function')
print(X)
# Deleting a vector
# Creating a vector
X <- c(5, 2, 1, 6)
# Deleting a vector
X <- NULL
print('Deleted vector')
print(X)
# Matrix Operations
# Creating a matrix
A= matrix (data=c(1,2,3,4,5,6),nrow=2,ncol=3)
print(A)
A= matrix (data=1:6,inrow=2,ncol=3)
print(A)
# Accessing a matrix
# accessing (2,3) element of A
print(A[2,3])
# accessing 2nd row of A
print(A[2,])
# accessing 3rd column of A
print(A[ ,3])
# Built-in Functions
# Find sum of numbers 4 to 6.
print(sum(4:6))
# Find max of numbers 4 and 6.
print(max(4:6))
# Find min of numbers 4 and 6.
print(min(4:6))
# Find mean of numbers in x
x=c(1,2,3,4,5)
mean(x)
# Find standard deviation of numbers in y
y=c(2,4,6,8,10)
sd(y)
# Find median of numbers in z
z=c(1,3,5,7,9)
median(z)
# Find range of numbers in a
a=c(1,2,3,4,5,6,7,8,9,10)
range(a)
# Find repetition of (1,2,3) 5times
y=rep(c(1,2,3),5)
print(y)
# Data Frames
# SALARY DATAFRAME
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
#Print the data frame.
print(emp.data)
# MARKS DATAFRAME
x=c(50,60,70)
#marks in maths
y=c(70,50,40)
#marks in science
z=c(60,40,20)
#marks in marathi
marksheet=data.frame("maths"=x,"science"=y,"marathi"=z)
marksheet
# Frequency Distribution
Install.Package("plyr")("dataset")("count")("datatable")
x=c(2,3,4,5,6,7,2,4,2,7,5)
table(x)
# creating a dataframe
library(data.table)
data_table <- data.table(col1 = sample(6 : 9, 9 ,
replace = TRUE),
col3 = c(1, 4, 1, 2, 2,
2, 1, 2, 2))
print (data_table)
#Bar Graph
# Create the input vectors.
colors = c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
# Create the matrix of the values.
Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11), nrow = 3, ncol = 5, byrow = TRUE)
# Create the bar chart
barplot(Values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col =
colors)
# Add the legend to the chart
legend("topleft", regions, cex = 1.3, fill = colors))