Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Answer Script
##################################################
#Exploring Data: the basics
#Answer Script oct 2015
#beccawilson
###################################################

##################################################
#Reading in Data
##################################################
setwd("/home/rw13742/Documents/teaching/R/becca_R_course/sscm/")
#?read.csv
?read.csv
file1 = read.csv("ukgas.csv") 

###################################################
#Exploring the data
###################################################
file1 # this displays whole file1
head(file1) # this displays top of file1
tail(file1) # this displays bottom of file1
file1 [1,] # this displays 1st column file1
file1[1:5,] # displays rows 1:5 file1
file1[,1] # this displays 1st row file1
file1[,1:5] # this displays cols 1:5
file1[,'year'] 
file1 [,'qtr1']
year  = file1[,'year'] 
qtr1 = file1[,'qtr1']

####################################################
#plotting data
#####################################################
?plot
plot(x = year, y = qtr1) #default is type = 'p' or points
plot(x = year, y = qtr1, type = 'l') #plots as line
#search the manual for par function
?par
plot(x = year, y = qtr1, type = 'l', col = 'red')

#######################################################
#Writing the data
#######################################################
?png
#print to file your plot as .png
png(filename="plot1.png")
plot(x = year, y = qtr1, type = 'l', col = 'red')
dev.off()
?pdf
#print to file as a .pdf
pdf (file=output2, h=7, w=12)
plot(x = year, y = qtr1, type = 'l', col = 'red')
dev.off()

###############################################
#Beautifying plot
###############################################
qtr2 = file1[,'qtr2']
plot(x = year, y = qtr1, type = 'l', col = 'red')
points(x = year, y = qtr2, type = 'l', col = 'black')
legend(x = 'topleft', y = NULL, legend = c('qtr1', 'qtr2'), col = c('red', 'black'), lty = 1)

##################################################
#full plot
###################################################
qtr2 = file1[,'qtr2']
qtr3 = file1[,'qtr3']
qtr4 = file1[,'qtr4']
plot(x = year, y = qtr1, type = 'l', col = 'red')
points(x = year, y = qtr2, type = 'l', col = 'black')
points(x = year, y = qtr3, type = 'l', col = 'blue')
points(x = year, y = qtr3, type = 'l', col = 'green')
legend(x = 'topleft', y = NULL, legend = c('qtr1', 'qtr2','qtr3', 'qtr4'), col = c('red', 'black', 'blue', 'green'), lty = 1)
min(qtr3)
#answer is 84.8
max(qtr1)
plot(x = year, y = qtr1, type = 'l', col = 'red', ylim=c(80,1200))
points(x = year, y = qtr2, type = 'l', col = 'black')
points(x = year, y = qtr3, type = 'l', col = 'blue')
points(x = year, y = qtr4, type = 'l', col = 'green')
legend(x = 'topleft', y = NULL, legend = c('qtr1', 'qtr2','qtr3', 'qtr4'), col = c('red', 'black', 'blue', 'green'), lty = 1)

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.