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

r file code

The document provides an extensive overview of various operations and functions in R programming, including mathematical calculations, data structures, data manipulation, and data visualization techniques. It covers topics such as vectors, matrices, lists, data frames, and statistical analysis, along with examples of plotting and regression analysis. Additionally, it discusses importing and exporting data, as well as time series analysis.

Uploaded by

Yogendra Singh
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)
4 views

r file code

The document provides an extensive overview of various operations and functions in R programming, including mathematical calculations, data structures, data manipulation, and data visualization techniques. It covers topics such as vectors, matrices, lists, data frames, and statistical analysis, along with examples of plotting and regression analysis. Additionally, it discusses importing and exporting data, as well as time series analysis.

Uploaded by

Yogendra Singh
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/ 16

#addition of two numbers

2+3

#square root of 2

sqrt(2)

#natural log of 4

log(4)

#exponential of 2

exp(2)

#value of sin of 20 in radians

sin(20)

#simple division

2/34

#value of pi

pi

#assigning values to variables and then performing operations

a=5

a^2

b<-a

b^2

#listing variables

ls()

#removing a variable/placeholder

rm(a)

ls()

#Example-1 Print every item in a list.


fruits <- list("apple","banana","cheery")

for(x in fruits){

print(x)}

#Example-2 stop the loop at the cheery.

fruits <- list("apple","banana","cheery")

for(x in fruits){

if(x=="cheery"){break}

print(x)}

#Example-3 Skip banana

fruits <- list("apple","banana","cheery")

for(x in fruits){

if(x=="banana"){next}

print(x)}

#Data structure in R

#We use the c() function to enter a vector.

a <- c(2,3,5)

#A vector can contains character strings.


b <- c("alpha","beta","gamma")

length(a)

length(b)

# Create a vector from 1 to 5.

x <- 1:5

y = c(1,3,5,7,9)

#Two vectors of the same length may be added/subtracted/multiplied/divided.

x+y

x-y

x+10

x-10

x/2

x/y

sqrt(y)

log(x)

x*y

# Extract all elements of y except the third element.

y[-3]

# Extract the third element of y.

y[3]

# Extract the first three elements of y.

y[1:3]
# Extract all except 1st and 5th.

y[-c(1,5)]

# Extract 1st and 5th elements of y.

y[c(1,5)]

# Extract elements of y with value less than 6.

y[y<6]

seq(1,7)

#seq() produces a sequence of numbers.

#Produce a sequence of numbers from 1 to 7 at a distance of 0.5.

seq(from=1,to=7)

#Stepsize 2

seq(from=1,to=7,by=2)

# To find minimum and maximum values of a vector.

a <- c(1,-2,3,-4)

b <- c(-1,2,-3,4)

min(a,b)#minimum values between a&b.

pmin(a,b)# minimum values term by term.

max(a)

min(a)

sum(a)

u <- c(10,20,30)

v <- c(1,2,3,4,5,6,7,8,9)

u+v

#Matrices
# A matrix is created using matrix() function.

matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=TRUE)

byrow command sequentiatly arranges elements by rowwise.

matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=FALSE)

A= matrix(c(1,2,3,4,5,6,7,8,9,10),nrow=2,ncol=5,byrow=TRUE)

A[2,3]# Extract the element in row 2 and column 3.

A[2,]# Extract row 2.

A[,4] # Extractcolumn 4.

A[,c(1,3)] #Extract columns 1 and 3.

dimnames(A)=list(c("row1","row2"),c("col1","col2","col3","col4","col5"))

A= matrix(c(1,2,3,4,5,6,7,8,9,10),nrow=2,ncol=5,byrow=TRUE,

A*10

dimnames = list(c("row1","row2"),c("col1","col2","col3","col4","col5")))

# Transpose of A

t(A)

mat <- matrix(c(1,2,3,4),nrow=2,byrow=TRUE)

mat

det(mat)#Determinant of a matrix.

A= matrix(c(1,2,3,4,5),nrow=3,ncol=2,byrow=TRUE)

B= matrix(c(11,12,13,14,15),nrow=3,ncol=2,byrow=TRUE)

A+B

A-B

A*B
A/B

# To create a list use list() function.

list1 <- list("apple","banana","cheery")

list1

list1[1]# 1st element

list[1] <- "blackcurrent"

list1

list1[2:3]

# Add orange to the list1

append(list1,"orange")

append(list1,"orange",after=2) # adds orange after index 2.

# Remove list item.

newlist <- list1[-1]

newlist

# concatenate two list.

list1 <- list("a","b","c")

list2 <- list(1,2,3)

list3 <- c(list1,list2)

list3

#Dataframes

#Data frames are data displayed in a format as a table . It is a list of vectors of equal length.

# Use data.frame() to create a data frame

n=c(2,3,5)

s=c("english","economics","hindi")

b=c(TRUE,FALSE,TRUE)
df=data.frame(n,s,b)

df

#Example-2

Data_frame <- data.frame(training=c("strength","stamina","other"),

Data_frame

pulse= c(100,50,120),duration=c(60,30,45))

Data_frame[1] # access the col1

Data_frame[["training"]]

Data_frame $ training

# Add row

Data_frame1 <- rbind(Data_frame,c("strength",110,110))

Data_frame1

Add column using cbind()

Data_frame2 <- cbind(Data_frame,steps= c(100,200,300))

Data_frame2

# Remove rows and columns

Data_frame3 <- Data_frame[-c(1),-c(2)]

Data_frame3

dim(Data_frame)

ncol(Data_frame)

nrow(Data_frame)

# Factors are used to categorize data

#Example

music <- factor(c("jazz","rock", "classic","classic","pop"))


music

levels(music)

length(music)

music[3]

music[3] <- "pop"

music[3]

#Data types in R

# 1) logical 2) numeric 3) integer 4) complex 5) character

a1=TRUE

class(a1)

a2=FALSE

class(a2)

a3 <- 2.45

class(a3)

a4 <- 12

class(a4)

a5 <- 2+3i

class(a5)

a6="hi"

class(a6)

a7=hi

country<-c("China","India","US","Indonesia","Pakistan")

population_2022<-c("142764786","1352642280","327096265","267670543","212228286")

population_2023<-c("1433783686","1366417754","320064917","270625568","216565318")

change_in_percentage<-c("+0.43%","+1.02%","+0.60%","+1.10%","2.04%")
data<-data.frame(country,population_2022,population_2023,change_in_percentage)

print(data)

install.packages("readxl")

library("readxl")

#Reading data from an excell file

getwd()

setwd("C:/Users/MATHS LAB/Documents/vikas")

getwd()

data<-read_excel("stats.xlsx",sheet=1) -/

print(data)

# A tibble: 5 ×4

df<-read.table("data.txt",header=TRUE)

#to view data from

print(df)

dim(df)

#write table

df<-data.frame(var1=c(1,2,3,4,5),var2=c(2,3,4,5,6),var3=c(5,6,7,8,9))

df

#export data to desktop(your folder)

write.table(df,"C:/Users/MATHS LAB/Documents/vikas/data2.txt")

#Data visualization is the technique to deliver insights in data using visuals such as graphs charts

# The plot() function is used to draw points (markers) in the diagram

plot(0.5,0.5)

plot(c(1,2,3,4),c(1,4,9,16))
x=seq(-pi,pi,0.1)

y=sin(x)

plot(x,y)

plot(1:10,type="l")

plot(1:10,type="l",col="blue")

# Scatter plot

# a scatter plot is a type of plot used to display the relationship between two numerical variables and
plot on dot for each

observation.

#the basic syntax for scatterplot is:

#plot(x,y,main,x|ab,y|ab,xlim,ylim,axes)

table(mtcars$wt)

plot(x=mtcars$wt,y=mtcars$mg,main="Weight vs Milage",xlab="Weight",ylab="Milage",col="blue")

H<-c(7,12,28,3,41)

M<-c("Mar","Apr","May","Jun","Jul")

#To give chart file a name

#Plot bar chart

barplot(H,names.arg=M)

#pie chart

#use the pie() function to draw a pie chart

x<-c(21,62,10,53)

labels <- c("London","New York","Singapore","Mumbai")

colors <- rainbow(length(slices))

# Create the pie chart

pie(x,labels)

#Histogram
value=c(10,15,17,20,24,26,30)

hist(value)

#box Plot

boxplot(value)

scores=scan()

1: 81 81 96 77

5: 95 98 73 83

9: 80 86 89 60

13: 79 62

15:

Read 14 items

range(scores)

median(scores)

mean(scores)

sd(scores)

hist(scores)

#ggplot is an R package which is designed especially for data visualization and providing exploratotry
data analysis.

#Use the function ggplot() to generate the plots.

#Syntax

#ggplot(data=<DATA>,mapping=aes(<MAPPINGS>))+<geom_function>()

# to load ggplot2

library(ggplot2)

#read in dataset

data(iris)

iris
ggplot(data=iris)

ggplot(data=iris,aes(y=Sepal.Length,x=Petal.Length,col=Species))+geom_point()

#Bar Plot and Histograms

# A bar count plot for "cyl" in mpg data

p<-ggplot(mpg,aes(x=factor(cyl)))+geom_bar(stat='count')

# A histogram count plot for "hwy" in mpg data

ggplot(data=mpg,aes(x=hwy))+geom_histogram(col="red",fill="green",alpha=0.2,bindwidth=5)

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# PieChart

df<-as.data.frame(table(mpg$class))

colnames(df)<-c("class","freq")

df

ggplot(df,aes(x="",y=freq,fill=factor(class)))+geom_bar(width=1,stat="Identity")+coord_polar(theta="y",
start=0)

#MEAN mean(x,trim=0,na.rm=FALSE,...)

#Create a vector

x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

xmean<-mean(x)

xmean

x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

xmean<-mean(x,trim=0.3) #When trim = 0.3, 3 values from each end will be dropped from the
calculations to find mean.

xmean
x<-c(-1,-3,1,0,2,5,4,7,6,3,8,-2,9,NA)

xmean<-mean(x)

xmean

xmean<-mean(x,na.rm=TRUE)

xmean

x<-c(-1,-3,1,0,2,5,4,7,6,3,8,-2,9,NA)

xmedian<-median(x)

xmedian

x<-c(-1,-3,1,0,2,5,4,7,6,3,8,-2,9,ΕΑ )

xmedian<-median(x,na.rm=TRUE)

xmedian

install.packages("readxl")

library("readxl")

getwd()

setwd("C:/Users/hp/Downloads")

getwd()

data <-read_excel('BPData.xlsx',sheet=1)

View(data)

data

# A tibble: 10 ×3

str(data) #gives summary of data

tibble [10 ×3] (S3: tbl_df/tbl/data.frame)

$ AGE : num [1:10] 25 30 33 35 37 40 41 43 44 48

$ WEIGHT : num [1:10] 82 142 66 113 123 147 115 178 115 116

$ BLOOD PRESSURE: num [1:10] 89 151 37 127 65 96 103 194 176 74


head(data) #gves first six rows of the data

# A tibble: 6 ×3

tail(data) #last six rows

# A tibble: 6 ×3

mean(data$WEIGHT)

median(data$WEIGHT)

range(data$WEIGHT)

IQR(data$WEIGHT)

sd(data$WEIGHT)

summary(data$WEIGHT)

Min. 1st Qu. Median Mean 3rd Qu. Max.

66.0 113.5 115.5 119.7 137.2 178.0

summary(data)

hist(data$WEIGHT,col="steelblue")

hist(data$'BLOOD PRESSURE')

qqnorm(data$'BLOOD PRESSURE')

library(moments)

#calculate skewness

skewness(data$WEIGHT)

x<-c(40,41,42,43,50)

hist(x)

print(skewness(x))

x<-c(10,11,21,22,23,25)

hist(x)

print(skewness(x))
#Regression analysis in R

#linear Regression

#Syntax:lm(formula data)

#Example predicting weight of a person when height is known

x=c(151,174,138,186,128,136,179,163,152,131)

y=c(63,81,56,91,47,57,76,72,62,48)

relation=lm(y~x)# gives relation between x and y

print(relation)

Call:

lm(formula = y ~ x)

#prediction finding syntax

#predict(object,newdata)

#find weight of a person with height 170

a=data.frame(x=170)

result=predict(relation,a)

print(result)

cor.test(x,y) # to find pearson's product-moment correlation

pearson's product-moment correlation

data: x and y

t = 12.997, df = 8, p-value = 1.164e-06

alternative hypothesis: true correlation is not equal to 0

95 percent confidence interval:

0.9031373 0.9947558

sample estimates:

cor
0.9771296

# time series analysis in R

#it is used to see how an object behaves over a period of time

#Syntax: object name=ts(data,start,end,frequency)

#converting to time series

sales=c(79,11,74,86,65,23,45,49,99,24,40,48,51)

T1=ts(sales)

T1

time Series:

Start = 1

End = 13

Frequency = 1

plot(T1)

sales.ts=ts(sales,start=2018,frequency=12)

sales.ts

stockrate=c(480,968,274,492,771,968,961,236,208,381,927,140,197)

#example: stock rate over a year

stockrate=c(480,968,274,492,771,968,961,236,208,381,927,140,197)

stockrate.ts=ts(stockrate,start=2019,frequency=12)

print(stockrate.ts)

plot(stockrate.ts)

You might also like