0% found this document useful (0 votes)
44 views24 pages

WEEK-1 Aim: Download and Install R-Programming Environment and Install Basic Packages Using

The document discusses installing and learning the basics of R programming. It outlines steps to download and install R and packages. It then covers data types in R, creating and accessing variables, arithmetic, relational and logical operators, and assignment operators. Examples are provided for each topic. Various R programming constructs like loops, functions, reading files and manipulating data frames are also demonstrated with sample code and expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views24 pages

WEEK-1 Aim: Download and Install R-Programming Environment and Install Basic Packages Using

The document discusses installing and learning the basics of R programming. It outlines steps to download and install R and packages. It then covers data types in R, creating and accessing variables, arithmetic, relational and logical operators, and assignment operators. Examples are provided for each topic. Various R programming constructs like loops, functions, reading files and manipulating data frames are also demonstrated with sample code and expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Shaik Reshma

17131A05K1
CSE-4
WEEK-1
Aim:Download and install R-Programming environment and install basic packages using
install.packages() command in R.
1.Download and install R-Programming
Step – 1: Go to CRAN R project website.

Step – 2: Click on the Download R for Windows link.


Step – 3: Click on the base subdirectory link or install R for the first time link.

Step – 4: Click Download R X.X.X for Windows (X.X.X stand for the latest version of R. eg:
3.6.1) and save the executable .exe file.

Step – 5: Run the .exe file and follow the installation instructions.
Shaik Reshma
17131A05K1
CSE-4

5.a. Select the desired language and then click Next.

5.b. Read the license agreement and click Next.

5.c. Select the components you wish to install (it is recommended to install all the components).
Click Next.
Shaik Reshma
17131A05K1
CSE-4

5.d. Enter/browse the folder/path you wish to install R into and then confirm by clicking Next.

5.e. Select additional tasks like creating desktop shortcuts etc. then click Next.
Shaik Reshma
17131A05K1
CSE-4

5.f. Wait for the installation process to complete.

5.g. Click on Finish to complete the installation.


Shaik Reshma
17131A05K1
CSE-4

2.Install packages:

2.1.Install directly from CRAN


install.packages("Package Name")

# Install the package named "XML".


install.packages("XML")

2.2.Install package manually


install.packages(file_name_with_path, repos = NULL, type = "source")

# Install the package named "XML"


install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")
Shaik Reshma
17131A05K1
CSE-4
WEEK-2
Aim:Learn all the basics of R-Programming (Data types, Variables, Operators etc,.)
1.Data Types:
1.1.Logical-TRUE, FALSE
> v <- TRUE
> print(class(v))
[1] "logical"

1.2.Numeric-12.3, 5, 999
> v <- 23.5
> print(class(v))
[1] "numeric"

1.3.Integer-2L, 34L, 0L
> v <- 2L
> print(class(v))
[1] "integer"

1.4.Complex-3 + 2i
> v <- 2+5i
> print(class(v))
[1] "complex"

1.5.Character-'a' , '"good", "TRUE", '23.4'


> v <- "TRUE"
> print(class(v))
[1] "character"

1.6.Raw-"Hello" is stored as 48 65 6c 6c 6f
> v <- charToRaw("Hello")
> print(class(v))
[1] "raw"
>v
[1] 48 65 6c 6c 6f

2.Variables:
A valid variable name consists of letters, numbers and the dot or underline characters. The
variable name starts with a letter or the dot not followed by a number.
2.1.Variable Assignment:
> # Assignment using equal operator.
> var.1 = c(0,1,2,3)
> # Assignment using leftward operator.
> var.2 <- c("learn","R")
> # Assignment using rightward operator.
> c(TRUE,1) -> var.3
> print(var.1)
[1] 0 1 2 3
> print(var.2)
[1] "learn" "R"
> print(var.3)
Shaik Reshma
17131A05K1
CSE-4

[1] 1 1

2.2.Finding Variables
To know all the variables currently available in the workspace we use the ls() function. Also the
ls() function can use patterns to match the variable names.
>print(ls())

2.3.Deleting Variables:
> rm(var.3)
> print(var.3)
Error in print(var.3) : object 'var.3' not found

3.Operators
3.1.Arithmetic Operators
+ Adds two vectors
> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v+t)
[1] 10.0 8.5 10.0

− Subtracts second vector from the first


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v-t)
[1] -6.0 2.5 2.0

* Multiplies both vectors


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v*t)
[1] 16.0 16.5 24.0

/ Divide the first vector with the second


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v/t)
[1] 0.250000 1.833333 1.500000
Shaik Reshma
17131A05K1
CSE-4

%% Give the remainder of the first vector with the second


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v%%t)
[1] 2.0 2.5 2.0

%/% The result of division of first vector with second (quotient)


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v%/%t)
[1] 0 1 1

^ The first vector raised to the exponent of second vector


> v <- c( 2,5.5,6)
> t <- c(8, 3, 4)
> print(v^t)
[1] 256.000 166.375 1296.000

3.2.Relational Operators
> Checks if each element of the first vector is greater than the corresponding element of the
second vector.
> v <- c(2,5.5,6,9)
> t <- c(8,2.5,14,9)
> print(v>t)
[1] FALSE TRUE FALSE FALSE

< Checks if each element of the first vector is less than the corresponding element of the
second vector.
> v <- c(2,5.5,6,9)
> t <- c(8,2.5,14,9)
>print(v < t)
[1] TRUE FALSE TRUE FALSE

== Checks if each element of the first vector is equal to the corresponding element of the
second vector.
> v <- c(2,5.5,6,9)
> t <- c(8,2.5,14,9)
>print(v == t)
[1] FALSE FALSEFALSE TRUE

<= Checks if each element of the first vector is less than or equal to the corresponding element
of the second vector.
> v <- c(2,5.5,6,9)
> t <- c(8,2.5,14,9)
> print(v<=t)
[1] TRUE FALSE TRUE TRUE

>= Checks if each element of the first vector is greater than or equal to the corresponding
element of the second vector.
Shaik Reshma
17131A05K1
CSE-4

> v <- c(2,5.5,6,9)


> t <- c(8,2.5,14,9)
> print(v>=t)
[1] FALSE TRUE FALSE TRUE

!= Checks if each element of the first vector is unequal to the corresponding element of the
second vector.
> v <- c(2,5.5,6,9)
> t <- c(8,2.5,14,9)
>print(v!=t)
[1] TRUETRUETRUE FALSE

3.3.Logical Operators
&
> v <- c(3,1,TRUE,2+3i)
> t <- c(4,1,FALSE,2+3i)
> print(v&t)
[1] TRUETRUE FALSE TRUE

|
> v <- c(3,0,TRUE,2+2i)
> t <- c(4,0,FALSE,2+3i)
> print(v|t)
[1] TRUE FALSE TRUE TRUE

!
> v <- c(3,0,TRUE,2+2i)
>print(!v)
[1] FALSE TRUE FALSE FALSE

&&
> v <- c(3,0,TRUE,2+2i)
> t <- c(1,3,TRUE,2+3i)
> print(v&&t)
[1] TRUE

||
> v <- c(0,0,TRUE,2+2i)
> t <- c(0,3,TRUE,2+3i)
> print(v||t)
[1] FALSE

3.4.Assignment Operators
<− or = or <<− Called Left Assignment
> v1 <- c(3,1,TRUE,2+3i)
> v2 <<- c(3,1,TRUE,2+3i)
> v3 = c(3,1,TRUE,2+3i)
Shaik Reshma
17131A05K1
CSE-4

> print(v1)
[1] 3+0i 1+0i 1+0i 2+3i
> print(v2)
[1] 3+0i 1+0i 1+0i 2+3i
> print(v3)
[1] 3+0i 1+0i 1+0i 2+3i
->or ->> Called Right Assignment
>c(3,1,TRUE,2+3i) -> v1
>c(3,1,TRUE,2+3i) ->> v2
> print(v1)
[1] 3+0i 1+0i 1+0i 2+3i
> print(v2)
[1] 3+0i 1+0i 1+0i 2+3i
Shaik Reshma
17131A05K1
CSE-4

WEEK-3
Aim:Write a program to find a list of even numbers from 1 to n using R-Loops.
Code:
n=as.integer(readline())
a=c(1:n)
for(i in a){
if(i%%2==0){
print(i)
}
}

Output:
Shaik Reshma
17131A05K1
CSE-4

WEEK-4
Aim:Create a function to print squares of numbers in sequence.
Code:
square=function(n){
a=c(1:n)
for(i in a){
f=i*i
print(f)
}
}
n=as.integer(readline())
square(n)

Output:
Shaik Reshma
17131A05K1
CSE-4

WEEK-5
Aim:Write a program to join columns and rows in a data frame using cbind() and rbind() in R.
Code:
mat=matrix(c(1:10),ncol=5,byrow=FALSE)
mat=cbind(mat,c(1:2))
print(paste("after cbind"))
print(mat)
mat=rbind(mat,c(1:6))
print(paste("after rbind"))
print(mat)

Output:
Shaik Reshma
17131A05K1
CSE-4

WEEK-6
Aim:Implement different String Manipulation functions in R.
Code:
1.Paste function:
str=paste("hello","world")
print(str)

str<- paste(c(1:3), "4", sep = ":")


print (str)

2.cat() function:
str<- cat("learn", "code", "tech", sep = ":")
print (str)

3.length() function:
print(length(c("hello","world")))

4.nchar() function:

5.toupper() function:

6.tolower() function

7.casefold() function

8.character replacement:
Shaik Reshma
17131A05K1
CSE-4

9.Splitting the string:

10.substring:
Shaik Reshma
17131A05K1
CSE-4

WEEK-7
Aim:-Implement different data structures in R (Vectors, Lists, DataFrames)
1.Vectors
Code:
> x = c(1,2,3,4,5)
> print(x)
>x[1:3]
Output:
[1] 1 2 3 4 5
[1] 1 2 3
2.Lists:
Code:
> Name = c("A","B","C","D")
>Roll_Num = c(1,2,3,4)
> Marks = c(96,97,98,99)
>student = list(Name, Roll_Num, Marks)
> print(student)
Output:
[[1]]
[1] "A" "B" "C" "D"
[[2]]
[1] 1 2 3 4
[[3]]
[1] 96 97 98 99

3.Data Frames:
Code:
> name = c("A","B","C","D")
>rollnum = c(1,2,3,4)
>marks = c(96,97,98,99)
>df = data.frame(name, rollnum, marks)
> df
Shaik Reshma
17131A05K1
CSE-4

WEEK-8
Aim:Write a program to read a csv file and analyze the data in the file in R.
Code:
Reading data from csv file:
data=read.csv(“input.csv”)
print(data)

By default the read.csv() function gives the output as a data frame. This can be easily checked
as follows. Also we can check the number of columns and rows:

Get the maximum:

Get details regarding maximum salary using subset function:


print(subset(data,salary==max(salary)))

info<- subset(data, salary > 600 & dept == "IT")


print(info)
Shaik Reshma
17131A05K1
CSE-4

WEEK-9
Aim:Create pie chart and bar chart using R.
Creating pie chart:
Syntax
The basic syntax for creating a pie-chart using the R is −
pie(x,labels,radius,main,col,clockwise)
Following is the description of the parameters used −
● x is a vector containing the numeric values used in the pie chart.
● labels are used to give description to the slices.
● radius indicates the radius of the circle of the pie chart.(value between −1 and +1).
● main indicates the title of the chart.
● col indicates the color palette.
● clockwise is a logical value indicating if the slices are drawn clockwise or anti clockwise.
Code:
x <- c(21, 62, 10, 53)
labels <- c("India","US","UK","Canada")
pie(x,labels)
Output:

Pie Chart Title and Colors


Code:
x <- c(21, 62, 10, 53)
labels <- c("India","US","UK","Canada")
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))

Output:
Shaik Reshma
17131A05K1
CSE-4

Slice Percentages and Chart Legend


Code:
x <- c(21, 62, 10, 53)
labels <- c("India","US","UK","Canada")
piepercent<- round(100*x/sum(x), 1)
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
legend("topright", c("India","US","UK","Canada"), cex = 0.8,
fill=rainbow(length(x)))
Output:

3D Pie Chart
Code:
x <- c(21, 62, 10,53)
lbl<- c("India","Us","Uk","Canada")
pie3D(x,labels = lbl,explode = 0.1, main = "Pie Chart of Countries ")

Output:
Shaik Reshma
17131A05K1
CSE-4

Creating a bar chart:


Syntax:
The basic syntax to create a bar-chart in R is –
barplot(H,xlab,ylab,main,names.arg,col)
Following is the description of the parameters used −
● H is a vector or matrix containing numeric values used in a bar chart.
● xlab is the label for the x axis.
● ylab is the label for y axis.
● main is the title of the bar chart.
● names.arg is a vector of names appearing under each bar.
● col is used to give colors to the bars in the graph.
Code:
H <- c(7,12,28,3,41)
barplot(H)
Output:

Bar Chart Labels, Title and Colors:


Code:
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
Shaik Reshma
17131A05K1
CSE-4

barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="blue",
main="Revenue chart",border="red")
Output:
Shaik Reshma
17131A05K1
CSE-4

WEEK-10
Aim:Create a data set and do statistical analysis on the data using R.
Mean
It is calculated by taking the sum of the values and dividing with the number of values in a data
series.
The function mean() is used to calculate this in R
Syntax
The basic syntax for calculating mean in R is −
mean(x,trim=0,na,rm-FALSE,...)
Following is the description of the parameters used −
● x is the input vector.
● trim is used to drop some observations from both end of the sorted vector.
● na.rm is used to remove the missing values from the input vector.
Code:
x=c(1:10)
> print(mean(x))
[1] 5.5

Output:
[1] 5.5
Applying Trim Option
When a trim parameter is supplied, the values in the vector get sorted and then the required
numbers of observations are dropped from calculating the mean.When trim = 0.3, 3 values
from each end will be dropped from the calculations to find mean.
Code:
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)
>print(mean(x,trim=0.3))
Output:
[1] 5.55
Applying NA Option
If there are missing values, then the mean function returns NA.
To drop the missing values from the calculation use na.rm = TRUE. which means remove the
NA values.
Code:
x <- c(12,7,3,4.2,18,2,54,-21,8,-5,NA)
> # Find mean.
>result.mean<- mean(x)
Shaik Reshma
17131A05K1
CSE-4

>print(result.mean)
# Find mean dropping NA values.
>result.mean<- mean(x,na.rm = TRUE)
>print(result.mean)
Output:
[1] NA
[1] 8.22
Median:The middle most value in a data series is called the median. The median() function is
used in R to calculate this value.
Syntax
The basic syntax for calculating median in R is −
median(x,na.rm=FALSE)
Following is the description of the parameters used −
● x is the input vector.
● na.rm is used to remove the missing values from the input vector.
Code:
> x <- c(12,7,3,4.2,18,2,54,-21,8,-5)
> # Find the median.
>median.result<- median(x)
>print(median.result)
Output:
[1] 5.6
Mode
The mode is the value that has the highest number of occurrences in a set of data. Unlike
mean and median, mode can have both numeric and character data.
R does not have a standard in-built function to calculate mode. So we create a user function to
calculate the mode of a data set in R. This function takes the vector as input and gives the
mode value as output.
Code:
>getmode<- function(v) {
+ uniqv<- unique(v)
+ uniqv[which.max(tabulate(match(v, uniqv)))]
+}
> v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)> result <- getmode(v)
Shaik Reshma
17131A05K1
CSE-4

> print(result)
>charv<- c("o","it","the","it","it")
> result <- getmode(charv)
> print(result)
Output:
[1] 2
[1] "it"

You might also like