#How to create an image and save a pdf####
data<-read.csv("breakfast.csv")#Loads file
View(data)
data<-data[-c(1),]#Removes 1st row from top and displays data
data<-data[-c(2),]#Removes 2nd row from top and displays data
names <- c("Year", "FreeStudents", "ReducedStudents", "PaidStudents", "TotalStudents", "MealsServed", "PercentFree")
names
data<-read.csv("breakfast.csv",skip = 4,col.names = names)#Deletes 1st 4 rows and changes column headings to elements present in 'names'
head(data)
tail(data)
str(data)
data$MealsServed<-as.numeric(data$MealsServed)#Converting to numeric data type
str(data)
#PNG code starts from here####
png("result.png")#Creates a .png file
plot(data$TotalStudents~data$Year)#Creates a scatter plot between TotalStudents and Year and it's stored in png file
dev.off()#Prevents image from being displayed in Rstudio itself where there might be problems due to size
file.show("result.png")
#pdf code starts from here####
pdf("result.pdf")#Creates a .pdf file
plot(data$TotalStudents~data$Year)#Creates a scatter plot between TotalStudents and Year and it's stored in pdf file
dev.off()
file.show("result.pdf")
#Plot with more details####
png("result.png")
plot(data$TotalStudents~data$Year, main = "students details", xlab = "Year", ylab = "Total students", col = "red", type = "o")#Here graphic parameters are mentioned like title, xlabel, ylabel, plot color, type of points, etc.
lines(data$FreeStudents~data$Year, col = "blue", type = "o")#Creates a 2nd plot in same graph with different color and we can't use same plot command again as R only reads and executes one of them
lines(data$PaidStudents~data$Year, col = "green", type = "o")
#Another plot with green color
lines(data$ReducedStudents~data$Year, col = "pink", type = "o")
#Another plot with pink color
legend("topleft",c("TotalStudents","FreeStudents","PaidStudents","ReducedStudents"),fill = c("red", "blue", "green", "pink"))
#Displays legend in top left corner according to names and colors mentioned in order
dev.off()
file.show("result.png")
#Working with default data sets of R####
?ls#? before any command line gives all info on that command line
rm(list = ls())#This clears the environment
library()#Displays library packages
search()#Displays loaded packages
?reserved#Gives reserved keywords that can't be used as variables
help("reserved")#Same as above
history()#Returns all commands used since start of application
savehistory(file = "Day3hist.txt")#Saving history file
dir()
file.show("Day3hist.txt")
?datasets#Gives info on R datasets package
library(help = "datasets")#Gives lists of datasets
?Titanic#Give info on titanic
data<-Titanic
View(data)
data
data<-as.data.frame(data)#Converting table to data frame
data
str(data)
summary(data)
head(data)
tail(data)
length(data)#No of columns
length(data$Class)#No of rows
d1<-data[1:5,]#Display rows 1 to 5
View(d1)
summary(d1)
d2<-data[11:13,]#Display rows 11 to 13
View(d2)
d3<-rbind(d1,d2)#Join d1 and d2 and display all 8 rows
View(d3)
row.names(d3)
row.names(d3)<-c(1:8)#Change rows names to numbers from 1 to 8
View(d3)
sn<-c(1:32)#Serial numbers 1 to 32
sn
d4<-cbind(data,sn)#Joining serial numbers with data
d4<-cbind(data,sn,data,sn)#Multiple copies of same column is created in same data frame
View(d4)
?char
?cars
data1<-cars
View(data1)
?CO2
data2<-CO2
View(data2)