### R Basics 1 exercise answers (0.3 MB pptx)

```Basics
1
# 1.1 What is your current directory? This is the default directory.
getwd()
# 1.2 Change the working directory to your Rworkshop folder.
setwd("C:/Rworkshop")
# Note: check your current directory again.
getwd()
Open file from C:/.../R/R-3.1.1/etc and add: options(scipen=6)
# Note: test if this worked by closing and reopening R and copying/pasting the following code.
a <- 1234567891234.123456789
a
2
# 2.1 Create an object named x and assign it the value 8.
x <- 8
# 2.2 Create an object named a and assign it to the value pi. Display the value of a.
a <- pi
a
# 2.3 Create an object named b and assign it to the value "pi". Display the value of b.
b <- "pi"
b
# 2.4 What are the modes of a and b?
mode(a)
mode(b)
# 2.5 Using the object you created, a, create an object e that has the value  + .
e <- a + 1
# 2.6 Save e to a file named pi.Rda in your working directory, then remove e from your R
workspace, then check it is not in the R workspace.
save(e, file="pi.Rda")
rm(e)
ls()
3
# 2.7 Load e back to R workspace and check that it is there.
ls()
# 2.8 Remove all the objects you created and the file you stored e in.
rm("a","b","e","myObject", "x")
file.remove("pi.Rda")
ls()
4
# 3.1 Form a vector named j, consisting of the objects b, d and e, with names 'b', 'd' and 'e'.
j <- c(b, d, e)
names(j) <- c("b", "d", "e")
# 3.2 What is the mode of j? Check whether j is a vector (Hint: use is.******( ) function).
mode(j)
is.vector(j)
# 3.3 Add the elements of vector g named "b" and "d". Do the same for the vector j. Why
the error message for one and not the other? Change the mode of the elements in j to
numeric and try again.
g["b"] + g["d"]
j["b"] + j["d"]
as.numeric(j["b"]) + as.numeric(j["d"])
# 3.4 Replace the "d" element of the vector g with the sum of the elements "b" and "last" of
the vector g. What is the value of element "d" of the vector g?
g["d"] <- g["b"] + g["last"]
160
# 3.5 Clean up your workspace, remove all objects.
rm(list=ls())
ls()
5
# Run the following code to create vect1 and vect2:
vect1 <- c(rep(1,10), rep(2,10))
# Creates a vector named vect1, with length 20, where the first 10 elements are 1 and next 10
# elements are 2.
vect2 <- c(seq(1,30), seq(40,60))
# Creates a vector named vect2, which contains the numbers from 1 to 30 and numbers from
# 40 to 60.
# 4.1 Create a vector named vect3, which contains a sample of size 20, without replacement,
from vect2. Hint, use the function sample(). So we can compare answers, set seed to 673.
Display vect3.
set.seed(673, kind=NULL);
vect3 <- sample(vect2,20,replace=FALSE)
vect3
# 4.2 Add 1 to the first 10 elements of vect3 and 2 to the next 10 elements. Hint use vect1.
vect3 <- vect3 + vect1
vect3
# 4.3 How many elements in vect3 are greater than 25?
sum(vect3 > 25)
12
6
# 4.4 How many elements in vect3 are less than 50?
sum(vect3 < 50)
14
# 4.5 What is the max of the elements in vect3 which are less than 50?
max(vect3[vect3<50])
48
# 4.6 How many elements in vect3 are greater than 25 and less than 50?
sum(vect3 > 25 & vect3 < 50)
6
# 4.7 What is the mean of the elements in vect3 are greater than 25 and less than 50?
mean(vect3[vect3 > 25 & vect3 < 50])
39.16667
7
## Run the following code on the previous slides..
h <- c(d, e, f)
names(h) <- c("d","e","f")
# 5.1 What is the mode of i ? What is the mode of h?
mode(i)
mode(h)
# 5.2 What is the mode of i[1]? What is the mode of i[[1]]?
mode(i[1])
mode(i[[1]])
# 5.3 Print i[[2]] and h[2] and then compare their modes.
i[[2]]
h[2]
mode(i[[2]])
mode(h[2])
# 5.4 Print i[[3]] and h[3] and then compare their modes.
i[[3]]
h[3]
mode(i[[3]])
mode(h[2])
8
## Scenerio
# You collected data on one plot today. We need to compile the data in R for analysis.
# There were 5 trees:
pine, pine, walnut, walnut, alder
# The heights were:
100, 75, 120, 90, 50
# The tree status was:
# 6.1 Assign an object named 'tree' to a vector with 5 elements of the tree names.
tree <- c("pine", "pine", "walnut", "walnut", "alder")
# 6.2 Display the number of elements in the tree vector?
length(tree)
# 6.3 What is the mode of the tree vector?
mode(tree)
# 6.4 Display the number of elements in the tree vector that are "walnut".
length(tree[tree == "walnut"])
# 6.5 Create a vector with 5 elements of tree heights and assign it to an object named 'ht'.
ht <- c(100, 75, 120, 90, 50)
# 6.6 What is the mode of the ht vector?
mode(ht)
# 6.7 What is the maximum height of all 5 trees?.. what is the average height?
max(ht)
mean(ht)
9
# 6.8 Create another vector of 5 elements with tree status and assign to an object named 'status'.
status <- c("live", "live", "live", "dead", "live")
# 6.9 What is the mode of 'status'?
mode(status)
# 6.10 Change 'status' to a factor with 2 levels, 1:live; 2:dead and assign back to object 'status'.
# 6.11 Create a data frame object named 'treesdf', with the 3 vectors you just made,
'tree', 'ht', and 'status'.
treesdf <- data.frame(tree, ht, status)
# 6.12 What are the dimensions of the treesdf?
dim(treesdf)
# 6.13 What are the column names of the data frame?
names(treesdf)
# 6.14 What does the structure of treesdf look like?
str(treesdf)
# 6.15 You missed a tree. It was a dead pine tree with height of 30 meters.
Add it to treesdf and assign this new data frame to on object named 'treesdf2'.
treesdf2 <- rbind(treesdf, c("pine", 30, "dead"))
# 6.16 Save the object, treesdf2 to a file in your workspace named 'treesdf2.Rda'.
save(treesdf2, file="treesdf2.Rda")
10
```