Some of the exercises contain more than what was covered by the lecture to show you some of the common and nice features of R. If you can’t find the answer in the lecture slides or the help menus (or Google) I have also put out the solutions on the bottom of the page.

Exercise 1

Getting started

Matrix operations

1 2 4 4
3 5 3 4

Dataframe

Exercise 2

One of the big strengths of R is the easy and nice plotting functions. Here you can try some of them:

Exercise 3

It is very important that you know how to read text files from your computer into R.First, get your current working directory by typing getwd() and download mydataframe.txt from here and save it in your current working directory (or you can just paste in the text in a new file which you create on your computer).

Special exercise

Can you save the dataframe as tab separated text file?

Solutions

Exercise 1

Getting started

  • Calculate 10 times 3
10 * 3 
## [1] 30
  • Make a variable x equal to 5
  • Calculate 10 times x
x <- 5
10 * x
## [1] 50

Matrix operations

  • Make the matrix A equal to

1 2 4 4
3 5 3 4

A <- matrix(c(1, 2, 4, 4, 3, 5, 3, 4), ncol = 4, byrow = TRUE)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    4    4
## [2,]    3    5    3    4
  • Give A columns name by using the function colnames()
colnames(A) <- c("col1", "col2", "col3", "col4")
A
##      col1 col2 col3 col4
## [1,]    1    2    4    4
## [2,]    3    5    3    4
  • Add 3 to each element in the matrix and call this matrix B
B <- A + 3
B
##      col1 col2 col3 col4
## [1,]    4    5    7    7
## [2,]    6    8    6    7
  • Add A and B. What do you get?
A + B
##      col1 col2 col3 col4
## [1,]    5    7   11   11
## [2,]    9   13    9   11
  • Include a 5th column equal to [1,2] to your matrix A and call the new matrix D. Use the command cbind()
D <- cbind(A, c(1,2))
D
##      col1 col2 col3 col4  
## [1,]    1    2    4    4 1
## [2,]    3    5    3    4 2
  • Get the third column of D.
D[,3]
## [1] 4 3

Dataframe

  • There are built-in data frames examples in R. One of them are called mtcars
  • What does mtcars look like? If you write head(mtcars) you will only see the first 6 rows.
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
  • How big is the data frame? Use the function dim() or the functions nrow() and ncol().
dim(mtcars)
## [1] 32 11
ncol(mtcars)
## [1] 11
nrow(mtcars)
## [1] 32
  • What is the cell value from the first row, second column of mtcars?
mtcars[1,2]
## [1] 6
  • Could you get the same value by using row and column names instead? Which names?
mtcars["Mazda RX4", "cyl"]
## [1] 6

Exercise 2

  • Make a histogram of mpg(miles per gallon) in mtcars, use hist() (hint: use the ‘$’ sign to access mpg)
hist(mtcars$mpg)

  • Make a histogram of mpg in mtcars with more breaks. (hint: see the available options for the hist() function with f1 or ?hist(). Hint2: ‘breaks =’).
hist(mtcars$mpg, breaks = 20)

  • Make a boxplot of mpg in mtcars, use boxplot(). (To make a boxplot for each number of cylinders use: boxplot(mtcars$mpg ~ mtcars$cyl))
boxplot(mtcars$mpg)

boxplot(mtcars$mpg ~ mtcars$cyl)

  • Make a scatterplot of mpg in mtcars using plot()
plot(mtcars$mpg)