Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

STATS5020 - Introduction to R programming

Assignment 3

Task 1 - version 1 [trees - v1; a3t1] [10 marks in total]

1. [1 mark] The dataset trees .txt provides measurements of the diameter (in inches), height (in ft) and volume (in cubic ft) of timber in 31 felled black cherry trees. Use R to read in the le trees .txt correctly and save it as a data frame called trees.

2. [3 marks] We can get a rough estimate of a tree’s age without cutting it down and counting its rings. Such an estimate can be computed as the tree’s diameter (in centimetres) divided by its growth rate. Create a function called tree .age that receives the arguments diameter (diameter of the tree, in inches or centimetres), rate .of .growth (the rate of growth of the tree), and cm (logical, set by default to FALSE to indicate that diameter is measured in inches). If cm is FALSE, then your functions should update diameter to its value in centimetres. Your function should return the estimated age.

3. [2 marks] What is the average age of the cherry trees?  To answer this question, use your function tree .age to estimate the age of the 31 cherry trees with a rate of growth of 2.5 and report the average age.

4. [4 marks] We are interested in checking if a linear relationship between the diameter of cherry trees and the timber produced can be assumed and whether the height affects this relationship. To this end, we can categorise the variable Height and plot Diameter versus Volume using different colours for the categories of Height. Create a new column in the trees data frame called HeightGroup, which takes the value short if the height is less or equal than the median height, and tall if the height is greater than the median height. You can use the built-in function median to calculate the median height. Plot Diameter versus Volume using two different colours for the two categories of HeightGroup. Your plot should look like the one below (hint: you can use the argument title in the legend function to put a title to the legend).

## (a)

trees  =  read .table( !trees .txt ! ,  header  =  T)

## (b)

tree .age  = function(diameter,  rate .of .growth,  cm  =  FALSE){

if(!cm)

diameter  =  diameter*2.54

diameter/rate .of .growth

}

## (c)

mean (tree .age (trees$Diameter,  2.5))

##  [1]  13 .46036

## (d)

meH  =  median(trees$Height)

trees  =  transform(trees,  heightGroup  =  cut(Height,

breaks  =  c (-Inf ,  meH,  Inf),    labels  =  c ( !short ! ,  !tall !)))

plot(trees$Diameter,  trees$Volume,  col  =  unclass(trees$heightGroup),

xlab  =   !Diameter ! ,  ylab  =   !Volume !)

legend( !topleft ! ,  col  =  1 :2 ,  pch  =  rep ( 1 ,2),  c ( !Short ! ,  !Tall!),  title  =  !Height !)

Height

Short Tall



20

Diameter

Task 1 - version 2 [trees - v2; a3t1] [10 marks in total]

1. [1 mark] The dataset trees .txt provides measurements of the diameter (in inches), height (in ft) and volume (in cubic ft) of timber in 31 felled black cherry trees. Use R to read in the le trees .txt correctly and save it as a data frame called trees.

2. [3 marks] We can get a rough estimate of a tree’s age without cutting it down and counting its rings. Such an estimate can be computed as the tree’s diameter (in centimetres) divided by its growth rate. Create a function called tree .age that receives the arguments diameter (diameter of the tree, in inches or centimetres), rate .of .growth (the rate of growth of the tree), and cm (logical, set by default to FALSE to indicate that diameter is measured in inches). If cm is FALSE, then your functions should update diameter to its value in centimetres. Your function should return the estimated age.

3. [2 marks] What is the average age of the cherry trees?  To answer this question, use your function tree .age to estimate the age of the 31 cherry trees with a rate of growth of 1.88 and report the average age.

4. [4 marks] We are interested in checking if a linear relationship between the height of cherry trees and the timber produced can be assumed and whether the diameter affects this relationship. To this end, we can categorise the variable Diameter and plot Height versus Volume using different colours for the categories of Diameter. Create a new column in the trees data frame called DiameterGroup, which takes the value small if the diameter is less or equal than the median diameter, and big if the diameter is greater than the median diameter. You can use the built-in function median to calculate the median diameter. Plot Height versus Volume using two different colours for the two categories of DiameterGroup. Your plot should look like the one below (hint: you can use the argument title in the legend function to put a title to the legend).

## (a)

trees  =  read .table( !trees .txt ! ,  header  =  T)

## (b)

tree .age  = function(diameter,  rate .of .growth,  cm  =  FALSE){

if(!cm)

diameter  =  diameter*2.54

diameter/rate .of .growth

}

## (c)

mean (tree .age (trees$Diameter,  1.88))

##  [1]  17 .89942

## (d)

meD  =  median(trees$Diameter)

trees  =  transform(trees,  DiameterGroup  =  cut (Diameter,

breaks  =  c (-Inf ,  meD,  Inf),  labels  =  c ( !small ! ,  !big !)))

plot(trees$Height,  trees$Volume,  col  =  unclass(trees$DiameterGroup),

xlab  =   !Height ! ,  ylab  =   !Volume !)

legend( !topleft ! ,  col  =  1 :2 ,  pch  =  rep ( 1 ,2),  c ( !Small ! ,  !Big!),  title  =  !Diameter !)

Diameter

Small Big

85

Task 2 - version 1 [maxtemp - v1; a3t2] [10 marks in total]

1. [2 marks] The dataset tmax in the tmax1993 .Rdata le is a list of length 133 providing daily maximum temperature recorded at 133 stations. Each element of the list (i.e., each station) contains a data frame with the following information:

Variable       Description

year (1993)

month

day

z

lat,long latitude and longitude of the station

The list is named according to stationsIDs. Use R to read in the le tmax1993 .Rdata correctly.

2. [3 marks] Update the tmax list by removing the following columns for every station: julian, id, proc

and date. The updated tmax list should contain, for each station, a data frame with 6 columns (year, month, day, z, lat, lon).

3. [3 marks] Define df to be the data frame containing the 6 columns (year, month, day, z, lat and lon) for the 1st of May of 1993 and all the 133 stations (therefore, the data frame df should have 133 rows and 6 columns). Note that the columns year, month, and day will have exactly the same entries (as they all refer to the same date, 1st of May of 1993).

4. [2 marks] Plot all the locations in df using the maximum temperature (z) as a colour scale. Your plots should look like the one below.

## (a)

load( !tmax1993 .Rdata!)

## (b)

# Solution 1: using for()

for(i in 1:length(tmax)){

tmp  =  tmax[[i]]

tmax[[i]]  =  tmp[,  c ("year" ,  "month" ,  "day" ,  "z" ,  "lat" ,  "lon")]

}

# Solution 2: using apply()

rm (tmax) # to remove the object created using Solution 1

load( !tmax1993 .Rdata!) # load data again

tmax  =  lapply(tmax, function(x)  x[,  c ("year" ,  "month" ,  "day" ,  "z" ,  "lat" ,  "lon")])

## (c)

# Solution 1: using for()

df  =  NULL

for(i in 1:length(tmax)){

tmp  =  tmax[[i]][tmax[[i]]$month  ==  5  &  tmax[[i]]$day  ==  1 ,  ]

df  =  rbind(df,  tmp)

}

# Solution 2: using apply()

tmp  =  lapply(tmax, function(x)  x[x$month  ==  5  &  x$day  ==  1 ,  ])

df  =  data .frame(matrix(unlist(tmp),  nrow  =  length(tmp),  byrow  =  TRUE))

names (df)  =  names (tmp[[1]])

# (d)

plot(df$lon,  df$lat,  col  =  df$z,  pch  =  16 ,  xlab  =   !Longitude ! ,

ylab  =  !Latitude ! ,  main  =  ! 1st  of  May,  1993 !)