r file code
r file code
2+3
#square root of 2
sqrt(2)
#natural log of 4
log(4)
#exponential of 2
exp(2)
sin(20)
#simple division
2/34
#value of pi
pi
a=5
a^2
b<-a
b^2
#listing variables
ls()
#removing a variable/placeholder
rm(a)
ls()
for(x in fruits){
print(x)}
for(x in fruits){
if(x=="cheery"){break}
print(x)}
for(x in fruits){
if(x=="banana"){next}
print(x)}
#Data structure in R
a <- c(2,3,5)
length(a)
length(b)
x <- 1:5
y = c(1,3,5,7,9)
x+y
x-y
x+10
x-10
x/2
x/y
sqrt(y)
log(x)
x*y
y[-3]
y[3]
y[1:3]
# Extract all except 1st and 5th.
y[-c(1,5)]
y[c(1,5)]
y[y<6]
seq(1,7)
seq(from=1,to=7)
#Stepsize 2
seq(from=1,to=7,by=2)
a <- c(1,-2,3,-4)
b <- c(-1,2,-3,4)
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)
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[,4] # Extractcolumn 4.
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
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
list1
list1
list1[2:3]
append(list1,"orange")
newlist
list3
#Dataframes
#Data frames are data displayed in a format as a table . It is a list of vectors of equal length.
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
pulse= c(100,50,120),duration=c(60,30,45))
Data_frame[["training"]]
Data_frame $ training
# Add row
Data_frame1
Data_frame2
Data_frame3
dim(Data_frame)
ncol(Data_frame)
nrow(Data_frame)
#Example
levels(music)
length(music)
music[3]
music[3]
#Data types in R
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")
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)
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
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
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.
#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")
barplot(H,names.arg=M)
#pie chart
x<-c(21,62,10,53)
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.
#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()
p<-ggplot(mpg,aes(x=factor(cyl)))+geom_bar(stat='count')
ggplot(data=mpg,aes(x=hwy))+geom_histogram(col="red",fill="green",alpha=0.2,bindwidth=5)
# 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
$ WEIGHT : num [1:10] 82 142 66 113 123 147 115 178 115 116
# A tibble: 6 ×3
# A tibble: 6 ×3
mean(data$WEIGHT)
median(data$WEIGHT)
range(data$WEIGHT)
IQR(data$WEIGHT)
sd(data$WEIGHT)
summary(data$WEIGHT)
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)
x=c(151,174,138,186,128,136,179,163,152,131)
y=c(63,81,56,91,47,57,76,72,62,48)
print(relation)
Call:
lm(formula = y ~ x)
#predict(object,newdata)
a=data.frame(x=170)
result=predict(relation,a)
print(result)
data: x and y
0.9031373 0.9947558
sample estimates:
cor
0.9771296
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)
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)