Class Name: R Workshop

Exercise Name: Introduction to R

Student Name: Roger Federer

Student Version: 1

Number of questions: 11

Date and time of compilation: 2019-12-02 14:20:51 at Dell-Desktop

Notes:

• All exercises displayed here part of the book "Analyzing Financial and Economic Data with R", available in Amazon.com.
• All data files required for solving the exercises are available with Github package afedR. So, first install the package with command devtools::install_github('msperlin/afedR") and, as an example for reading a table from file "example.rds", simply type:

df <- readRDS(afedR::afedR_get_data_file('example.rds'))

Exercises

1. What is the name of the function that shows the representation of an object in R’s prompt ?

1. as.POSIXlt.character
2. cat
3. switch
4. print
5. is.function

2. From the list of file extensions presented below, what is the most likely file extension to be used with function source()?

1. .R
2. .Rproj
3. .csv
4. .RData
5. .Rmd

3. In a particular part of our code we used function load. What is extension of the file used as input in this function?

1. .R
2. .Rmd
3. .RData
4. .csv
5. .Rproj

4. Consider the following code.


my.idx <- -14:90
x <- my.idx[1]
y <- my.idx[length(my.idx)-2]


Without the execution of the code, what is the content of objects x and y? ?

1. x = -2, y = 100
2. x = -14, y = 88
3. x = 88, y = -14
4. x = -9, y = 78
5. x = -16, y = 98

5. The following code was executed in R.


my.vec <- runif(100)


Which of the following commands will result in an error?

1. my.vec*‘a’
2. my.vec[-1]
3. my.vec + runif(101)
4. my.vec[101]
5. my.vec + runif(100)

6. Consider the execution of the following R code:


rm(list = ls())
x=1:100
y=2:100
my.objs <- ls()


Which of the following commands will replicate the contents of my.objs?

1. NA
2. c(‘my.objs’)
3. x
4. y
5. c(‘x’, ‘y’)

7. Consider the following matrix M:

##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.47723007 0.09946616 0.33239467 0.8394404
## [2,] 0.73231374 0.31627171 0.65087047 0.3466835
## [3,] 0.69273156 0.51863426 0.25801678 0.3337749
## [4,] 0.47761962 0.66200508 0.47854525 0.4763512
## [5,] 0.86120948 0.40683019 0.76631067 0.8921983
## [6,] 0.43809711 0.91287592 0.08424691 0.8643395
## [7,] 0.24479728 0.29360337 0.87532133 0.3899895
## [8,] 0.07067905 0.45906573 0.33907294 0.7773207

If we created a new object called sol.q with the code:


my.nrows <- nrow(M)
my.ncols <- ncol(M)
sol.q <- M[my.ncols-1,my.ncols]

 

What would be its contents ? (You should be able to find the solution without any coding).

1. 0.662005076417699
2. 0.333774930797517
3. 0.692731556482613
4. 0.662005076417699
5. 0.477230065036565

8. Consider the creation of the following matrix M:

##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6422883 0.4100841 0.2702601 0.4781180
## [2,] 0.8762692 0.8108702 0.9926841 0.9240745
## [3,] 0.7789147 0.6049333 0.6334933 0.5987610
## [4,] 0.7973088 0.6547239 0.2132081 0.9761707
## [5,] 0.4552745 0.3531973 0.1293723 0.7317925

If we executed in R the commands dim(M)[1], ncol(M) and length(M) in that exact order, what would be the result?

1. 11, -4, 24
2. 8, -5, 24
3. 5, 4, 20
4. 4, 3, 20
5. 7, -3, 24

9. If we executed the following code in R, what would be the result?


my.f <- list.files(path = 'data', pattern = '*.csv',full.names = TRUE)


1. Object my.f will hold all filenames in folder data that have the .csv extension. It will also display the names of the subfolders
2. The object will return all files from folder data
3. The call to list.files will look for all filenames that start with data
4. Object my.f will hold all objects from namespace data
5. The object my.f will show the number of packages in CRAN in next week, 2019-12-09

10. A student executed the following R code:

set.seed(my.seed)
y <- sample(1:10, 3)
y <- y[c(1,3)]
x <- y[2]

If my.seed = 58, what is the content of object x?

1. NA
2. 2
3. 6
4. 3
5. 3

11. If you are working in a directory of your computer called “C:/MyCode” and want to change the directory to a subfolder called “data”, which of the following commands will do that?

1. setwd(paste0(getwd(),‘/data’))
2. getwd(paste0(setwd(),‘data’))
3. getwd(paste(getwd(),‘data’))
4. setwd(paste(getwd(),‘/data’))
5. setwd(getwd())