## Chapter 1: A Brief Introduction to R 

## Sec 1.1: An Overview of R 
## ss 1.1.1: A Short R Session 
##                      { R} must be installed! 
##             Using the console (or command line) window 
2+2 
## [1] 4
2*3*4*5          # * denotes 'multiply' 
## [1] 120
sqrt(10)         # the square root of 10 
## [1] 3.162
pi               # R knows about pi 
## [1] 3.142
2*pi*6378        # Circumference of earth at equator (km) 
## [1] 40074
3*4^ 
2 
## [1] 48
3*4^2; (3*4)^2 
## [1] 48
## [1] 144
##                 Entry of data at the command line 
Year <- c(1800, 1850, 1900, 1950, 2000) 
Carbon <- c(8, 54, 534, 1630, 6611) 
## Now plot Carbon as a function of Year 
plot(Carbon ~ Year, pch=16) 

plot of chunk unnamed-chunk-1

##              Collection of vectors into a data frame 
fossilfuel <- data.frame(year=Year, carbon=Carbon) 
fossilfuel         # Display the contents of the data frame. 
##   year carbon
## 1 1800      8
## 2 1850     54
## 3 1900    534
## 4 1950   1630
## 5 2000   6611
rm(Year, Carbon)     # The rm() function removes unwanted objects 

plot(carbon ~ year, data=fossilfuel, pch=16) 

plot of chunk unnamed-chunk-1

##        The R Commander Graphical User Interface (GUI) to R 
##      The working directory and the contents of the workspace 
getwd() 
## [1] "/Users/johnm1/r3/scripts"
ls() 
## [1] "fossilfuel" "metadata"
##                           Quitting { R} 
# q() 

##                      Installation of packages 
install.packages("DAAG") 
## 
## The downloaded binary packages are in
##  /var/folders/00/_kpyywm16hnbs2c0dvlf0mwr0000gq/T//RtmpWtVh3X/downloaded_packages
install.packages(c("magic", "schoolmath"), dependencies=TRUE) 
## 
## The downloaded binary packages are in
##  /var/folders/00/_kpyywm16hnbs2c0dvlf0mwr0000gq/T//RtmpWtVh3X/downloaded_packages
## ss 1.1.2: The uses of R 
##   { R} offers an extensive collection of functions and abilities 
range(fossilfuel$carbon) 
## [1]    8 6611
## 4 cities 
fourcities <- c("Toronto", "Canberra", "New York", "London") 
## display in alphabetical order 
sort(fourcities) 
## [1] "Canberra" "London"   "New York" "Toronto"
## Find the number of characters in "Toronto" 
nchar("Toronto") 
## [1] 7
## Find the number of characters in all four city names at once 
nchar(fourcities) 
## [1] 7 8 8 6
##        { R} will give numerical or graphical data summaries 
summary(cars) 
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
hist(cars$speed) 

plot of chunk unnamed-chunk-1

##            { R} is an interactive programming language 
celsius <- (0:4)*10 
fahrenheit <- 9/5*celsius+32 
conversion <- data.frame(Celsius=celsius, Fahrenheit=fahrenheit) 
print(conversion) 
##   Celsius Fahrenheit
## 1       0         32
## 2      10         50
## 3      20         68
## 4      30         86
## 5      40        104
## ss 1.1.3: Online Help 
?plot                # Equivalent to help(plot) 

apropos("sort")        # Try, also, apropos ("sor") 
## [1] "is.unsorted"  "sort"         "sort.default" "sort.int"    
## [5] "sort.list"    "sort.POSIXlt" "sortedXyData"
 # List all functions where "sort" is part of the name  
help.search("sort")    # Note that the argument is 'sort' 
 # List functions with 'sort' in the help page title or as an alias 

example(image) 
## 
## image> require(grDevices) # for colours
## 
## image> x <- y <- seq(-4*pi, 4*pi, len = 27)
## 
## image> r <- sqrt(outer(x^2, y^2, "+"))
## 
## image> image(z = z <- cos(r^2)*exp(-r/6), col  = gray((0:32)/32))

plot of chunk unnamed-chunk-1

## 
## image> image(z, axes = FALSE, main = "Math can be beautiful ...",
## image+       xlab = expression(cos(r^2) * e^{-r/6}))