Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Extra
- #.xlsx --> .csv
- #Opening csv file
- #data{#variable name} <- read.csv('filename.csv', header=TRUE)
- #Opening text file
- #data{#variable name} <- read.table('filename.txt', header=TRUE,sep=","{#Because data is seperated by ','s})
- #To clear console
- #Ctrl + L
- #---------------------------------------------------------------------------------------------------
- #Set Path
- setwd("F:\\SLIIT\\Practicals\\Year 02 Semester 02\\PS\\R")
- getwd()
- #Lab7 - 1
- #read data from the text file
- data<-read.table("lab7.txt",header=TRUE,sep=",")
- attach(data)
- fix(data)
- #Lab7 - 2
- #Summary/Strucure of data variable
- str(data)
- #Lab7 - 3
- #str - Used to count obesrvations
- #Observations = 517
- #Lab7 - 4
- min(wind)
- max(wind)
- #Lab7 - 5
- summary(temp)
- #Lab7 - 6
- boxplot(wind, horizontal = TRUE, outline = TRUE, pch=16)
- #pch = Symbols for representing outliers. ex:-8,16,24
- #Lab7 - 7(Negative,Positive,Normal)
- #Negative
- #Lab7 - 8
- median(temp)
- #Lab7 - 9
- mean(wind)
- sd(wind)
- sapply(data,sd)
- #Lab7 - 10
- IQR(wind)
- #Lab7 - 11
- #Create a frequency table with day &month count
- freq<-table(day, month)
- freq
- #Answer = 21
- #Lab7 - 12
- #Average = Mean
- mean(temp[month=='sep'])
- #Lab7 - 13
- barplot(freq,beside = TRUE,xlab = "Month",ylab="Frequency",legend=rownames(freq))
- #Beside = TRUE --> Bars in seperate
- #Beside = FALSE --> Bars in one
- #Population Mean
- mean(nicotine)
- #Variance
- var(nicotine)
- #Standard Deviation
- sd(nicotine)
- #Lab6 - 2
- #Sample values (5 samples)
- sam<-sample(nicotine,5)
- sam
- #5 numbers of random values
- #i is variable
- for(i in 1:30){
- s<-sample(nicotine,5)
- samples1<-cbind(samples1,s)
- n<-c(n,paste('s',i))
- }
- #Calculate the mean.
- s.mean<-colMeans(samples1)
- s.mean
- #Calculate variance(2 ways of finding variance.row vise and column vise)
- #row vise-->
- s.vars<-apply(samples1,1,var)
- s.vars
- #column vise
- s.vars1<-apply(samples1,2,var)
- s.vars1
- #Comparison of population mean and mean of sample means
- msm<-mean(s.mean)
- msm
- #Comparison of population variance and mean of sample variance of sample means
- vsm<-var(s.vars)
- vsm
- #Controll Statements
- team_a <-3
- team_b <-4 #Assigning values
- #this is a sample if else Statement
- if(team_a>team_b){
- print("Team A Wins")
- }else{
- print("Team B Wins")
- }
- setwd("F:\\SLIIT\\Practicals\\Year 02 Semester 02\\PS\\R")
- #Get the keyboard input from the keyboard
- #my.name is a variable u can use any word
- my.name <- readline(prompt = "Enter Name : ")
- my.age <- readline(prompt = "Enter age : ")
- if(myAge < 18){
- print("You are not major")
- }else{
- if(myAge >= 18 & myAge <= 60){
- print("You are eligeble to work")
- }
- else{
- print("Collect your Pension")
- }
- }
- #Frequency Table
- gender.freq<-table(Gender)
- gender.freq
- accomadation.freq<-table(Accomadation)
- accomadation.freq
- #Pie chart
- pie(gender.freq, main = "Pie chart for gender")
- pie(accomadation.freq, main = "Pie chart for accomadation")
- #Bar chart
- barplot(gender.freq,main="bar chart for Gender", ylab = "Frequency", col = "Red")
- #To get x axis
- abline(h=0)
- barplot(accomadation.freq,main="bar chart for Accomadation", ylab = "Frequency")
- #To get x axis
- abline(h=0)
- #Two way frequency table
- gender_accomadation.freq<-table(Accomadation,Gender)
- gender_accomadation.freq
- #Stack bar chart
- barplot(gender_accomadation.freq, main = "Gender and Accomadation",legend = rownames(gender_accomadation.freq))
- abline(h=0)
- #Clustered bar chart
- barplot(gender_accomadation.freq, beside = TRUE, main = "Gender and Accomadation",legend = rownames(gender_accomadation.freq))
- abline(h=0)
- #Histogram
- hist(attendance,main = "Histogram for Attendance",ylab = "Frequency")
- hist(salary,main = "Histogram for Salary",ylab = "Frequency")
- hist(years,main = "Histogram for Years",ylab = "Frequency")
- #Stem-Leaf Plot
- stem(attendance)
- stem(salary)
- stem(years)
- #Function for find the modes of a given set of values.
- get.mode<-function(x){
- counts<-table(x)
- names(counts[counts==max(counts)])
- }
- quantile(years)
- find.outliers<-function(x){
- q1<-quantile(x)[2]
- q3<-quantile(x)[4]
- iqr<-q3-q1
- ub<-q3+1.5*iqr
- lb<-q3-1.5*iqr
- print(paste("Upper Bound = ",ub))
- print(paste("Lower Bound = ",lb))
- print(paste("Outliers : ",paste(sort(x[x<lb | x>ub]),collapse = ",")))
- }
- find.outliers(years)
- sheep<-data.frame(height,weight)
- fix(sheep)
- #Exporting Data frames
- #Export Data to CSV and txt
- write.csv(sheep,file = "sheepNew.csv")
- write.table(sheep,file = "sheeptab1.txt")
- histogram<-hist(X2,main="Histogram for number of shareholders",breaks = seq(130,270,length=8))
- freq<-histogram$counts
- freq
- classes<-c()
- for(i in 1:length(breaks)-1){
- classes[i]<-paste("[", breaks[i],",", breaks[i+1],")")
- }
- #column bind - can be used two combine 2 data frames with the same values
- cbind(Classes = classes, Frequency = freq)
- #Draw frequency polygon in a new plot.(type = p for ponts and type = l for lines)
- plot(mids, freq, type = 'l', main = "Frequency Polygon for shareholders",xlab="shareholders",ylab="frequency",ylim = c(0,max(freq)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement