0% found this document useful (0 votes)
3 views

Practical 1- Basics of R

The document outlines a practical guide to the basics of R programming, covering topics such as data input, arithmetic operators, vector and matrix operations, data frames, frequency distribution, and graphical representations. It includes code examples for user input, arithmetic calculations, and data manipulation using R. Additionally, it provides instructions for creating various types of graphs and diagrams.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical 1- Basics of R

The document outlines a practical guide to the basics of R programming, covering topics such as data input, arithmetic operators, vector and matrix operations, data frames, frequency distribution, and graphical representations. It includes code examples for user input, arithmetic calculations, and data manipulation using R. Additionally, it provides instructions for creating various types of graphs and diagrams.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Group 1-(6101-Shweta Lad,6103-Ojas Mahaddalkar,6104-Saket Mandavkar,6105-Prajwal Mane,

6107-Prabhat Mishra,6171-Om Patkar- Participated;6102-Bhavesh Limbare,6106-Ayush Mayekar-


Not Participated)
References:- www.geeksforgeeks.org , www.tutorialspoint.com , www.statmethods.net and R
Studio’s Help
Practical 1 – Basics Of R
 Data Input, Arithmetic Operators
 Vector Operations, Matrix Operations
 Data Frames, Built-in Functions
 Frequency Distribution, Grouped Frequency Distribution
 Diagrams and Graphs

# 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)

# taking input with showing the message


var = readline(prompt = "Enter any number : ");
# convert the inputted value to an integer
var = as.integer(var);
# print the value
print(var)

# taking multiple inputs


# using braces
{
var1 = readline("Enter 1st number : ");
var2 = readline("Enter 2nd number : ");
var3 = readline("Enter 3rd number : ");
var4 = readline("Enter 4th number : ");
}
# converting each value
var1 = as.integer(var1);
var2 = as.integer(var2);
var3 = as.integer(var3);
var4 = as.integer(var4);
# print the sum of the 4 number
print(var1 + var2 + var3 + var4)

# taking string and character as input


# string input
var1 = readline(prompt = "Enter your name : ");
# character input
var2 = readline(prompt = "Enter any character : ");
# convert to character
var2 = as.character(var2)
# printing values
print(var1)
print(var2)

# taking input using scan()


x = scan()
# print the inputted values
print(x)

# 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

# string file input using scan()


# To Run getwd()-get working directory
s = scan("fileString.txt" ,what = " ")
# double file input using scan()
d = scan("fileDouble.txt", what = double())
# character file input using scan()
c = scan("fileChar.txt", what = character())
# print the inputted values
print(s) # string
print(d) # double
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)

# Arithmetic Operations on Vector


# Creating Vectors
X <- c(5, 2, 5, 1, 51, 2)
Y <- c(7, 9, 1, 5, 2, 1)
# Addition
A <- X + Y
print('Addition')
print(A)
# Subtraction
S <- X - Y
print('Subtraction')
print(S)
# Multiplication
M <- X * Y
print('Multiplication')
print(M)
# Division
D <- X / Y
print('Division')
print(D)
# Sorting a Vector
# Creating a Vector
X <- c(5, 2, 5, 1, 51, 2)
# Sort in ascending order
A <- sort(X,)
print('sorting done in ascending order')
print(A)
# sort in descending order.
B <- sort(X, decreasing = TRUE)
print('sorting done in descending order')
print(B)

# 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])

# R program for matrix addition


# using '+' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B + C)

# R program for matrix subtraction


# using '-' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B - C)

# R program for matrix multiplication


# using '*' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 1i, 0.1), nrow = 1, ncol = 3)
# Printing the resultant matrix
print (B * C)

# R program for matrix division


# using '/' operator
# Creating 1st Matrix
B = matrix(c(4, 6i, -1), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 2i, 0), nrow = 1, ncol = 3)
# Printing the resultant matrix
print(B / C)

# 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

# FRIENDS DATA FRAME


# creating a data frame
friend.data <- data.frame(
friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),stringsAsFactors = FALSE)
# print the data frame
print(friend.data)

# 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),

col2 = letters[1 : 3],

col3 = c(1, 4, 1, 2, 2,
2, 1, 2, 2))

print (data_table)

freq <- table(data_table$col1)

print ("Modified Frequency Table")


print (freq)

print ("Cumulative Frequency Table")


print (cumsum)

print ("Relative Frequency Table")


(prob)

#barplot for frequency distribution

dataset <- c("a","a","b","c","d","a","b","c","b","a")


dataset
library(plyr)
y <- count(dataset)
y
class(y)
barplot(Y)
y$freq
y$x
barplot(y$freq)
barplot(y$freq,names.arg = y$x,main ="frequence table of grades",col =
c("red","blue","green","yellow"))
n <- sum(y$freq)
n
rf <- y$freq/n
rf
barplot(rf,names.arg = y$x,main ="frequence table of grades",col = c("red","blue","green","yellow"))

# Diagrams and Graphs


#Pie Chart
# Data for the graph.
x = c(21, 62, 10,53)
labels = c("London","New York","Singapore","Mumbai")
# Formula to Calculate Percentage.
piepercent = round(100*x/sum(x), 1)
# Plot the chart.
# Pie Chart Plot.
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
# Legend Plot
legend("topright", c("London","New York","Singapore","Mumbai"),cex = 1.2, fill =
rainbow(length(x)))

#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))

You might also like