Introduction to R. Session 5.

# load the 'Orange growth' data set
data(Orange)

# get information on the 'Orange' data

# ?Orange

# convert 'age' to a date
secs.per.day <- 24 * 60 * 60

Orange$age.sec <- Orange$age * secs.per.day

Orange$Date <- as.POSIXct(Orange$age.sec, origin = "1968-12-31", tz = "EST")

# get the month and other elements out of the date
Orange$Month <- format(Orange$Date, "%m")
Orange$Day <- format(Orange$Date, "%j")
Orange$Day.of.Month <- format(Orange$Date, "%d")

# do some aggregation excersice
aggregate(Orange$circumference, by = list(Orange$Month), FUN = mean)
##   Group.1     x
## 1      04  88.2
## 2      05 145.6
## 3      09 134.2
## 4      10 133.3
aggregate(Orange$circumference, by = list(Orange$Day), FUN = mean)
##   Group.1     x
## 1     117  31.0
## 2     118  57.8
## 3     120 175.8
## 4     135 145.6
## 5     273 134.2
## 6     276 173.4
## 7     298  93.2
# instead of the automatically generated 'Group.1' and 'Group.2' names, we
# can specify what name the groupings should have.
aggregate(Orange$circumference, by = list(Month = Orange$Month, Day = Orange$Day.of.Month), 
    FUN = mean, na.rm = TRUE)
##   Month Day     x
## 1    10  02 173.4
## 2    05  14 145.6
## 3    10  25  93.2
## 4    04  27  31.0
## 5    04  28  57.8
## 6    04  30 175.8
## 7    09  30 134.2

# aggregate the whole data frame gives the usual warnings regarding
# parameters that are not numeric or logical
aggregate(Orange, by = list(Month = Orange$Month, Day = Orange$Day.of.Month), 
    FUN = mean, na.rm = TRUE)
## Warning: argument is not numeric or logical: returning NA Warning:
## argument is not numeric or logical: returning NA Warning: argument is not
## numeric or logical: returning NA Warning: argument is not numeric or
## logical: returning NA Warning: argument is not numeric or logical:
## returning NA Warning: argument is not numeric or logical: returning NA
## Warning: argument is not numeric or logical: returning NA Warning:
## argument is not numeric or logical: returning NA Warning: argument is not
## numeric or logical: returning NA Warning: argument is not numeric or
## logical: returning NA Warning: argument is not numeric or logical:
## returning NA Warning: argument is not numeric or logical: returning NA
## Warning: argument is not numeric or logical: returning NA Warning:
## argument is not numeric or logical: returning NA Warning: argument is not
## numeric or logical: returning NA Warning: argument is not numeric or
## logical: returning NA Warning: argument is not numeric or logical:
## returning NA Warning: argument is not numeric or logical: returning NA
## Warning: argument is not numeric or logical: returning NA Warning:
## argument is not numeric or logical: returning NA Warning: argument is not
## numeric or logical: returning NA Warning: argument is not numeric or
## logical: returning NA Warning: argument is not numeric or logical:
## returning NA Warning: argument is not numeric or logical: returning NA
## Warning: argument is not numeric or logical: returning NA Warning:
## argument is not numeric or logical: returning NA Warning: argument is not
## numeric or logical: returning NA Warning: argument is not numeric or
## logical: returning NA
##   Month Day Tree  age circumference   age.sec                Date Month
## 1    10  02   NA 1372         173.4 118540800 1972-10-03 10:00:00    NA
## 2    05  14   NA 1231         145.6 106358400 1972-05-15 10:00:00    NA
## 3    10  25   NA  664          93.2  57369600 1970-10-26 10:00:00    NA
## 4    04  27   NA  118          31.0  10195200 1969-04-28 10:00:00    NA
## 5    04  28   NA  484          57.8  41817600 1970-04-29 10:00:00    NA
## 6    04  30   NA 1582         175.8 136684800 1973-05-01 10:00:00    NA
## 7    09  30   NA 1004         134.2  86745600 1971-10-01 10:00:00    NA
##   Day Day.of.Month
## 1  NA           NA
## 2  NA           NA
## 3  NA           NA
## 4  NA           NA
## 5  NA           NA
## 6  NA           NA
## 7  NA           NA
# create a figure of growth over time
library(ggplot2)

p <- ggplot(data = Orange, aes(x = age, y = circumference))
p <- p + geom_point(aes(colour = Tree))
p <- p + geom_smooth(aes(colour = Tree), method = "lm", se = T)
p <- p + facet_grid(Tree ~ .)
p

plot of chunk unnamed-chunk-1


# then back to session 4 regarding figures, regression and dates
sec.in.a.day <- 24 * 60 * 60
Orange$Date <- Orange$age * sec.in.a.day
Orange$Date <- as.POSIXct(Orange$Date, tz = "EST", origin = "1968-12-31")