0% found this document useful (0 votes)
30 views5 pages

A1rib T4

Uploaded by

e.stephenson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

A1rib T4

Uploaded by

e.stephenson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 1

Instructions (please read carefully)

General information: This assignment focuses on building up your knowledge and


familiarity with R and RStudio as well as the logic of object-oriented statistical languages.

Submitting the assignment: Please use this word file as your starting point. Add your
answers in the boxes below the questions. Please also copy-paste the R code that you use if
the question asks you to do so. Once you have completed it, convert this word document to
pdf and submit the pdf as well as the R script that you used to come to the answers in Canvas
-> Assignments -> Assignment 1.

Remember to upload the files to Canvas on Thursday before 12h.

Please name the pdf document and the R script: “A1RIB_TeamName”. For example, if Team
A submitted the files, they would be named A1RIB_TA.pdf & A1RIB_TA.R. Each team
should submit only one file.

To check:

1. Make sure that you create a main folder for this assignment (you can name the folder
something like “A1_RIB” or whatever you like).
2. This folder can consist of sub-folders like code, data…
3. Set the working directory as your main folder (under Session -> Set Working
Directory).
4. Consult the R instructional videos and the “A very short introduction to R document
by Torfs & Brauer” to help you get started.

Questions

Basics

1. Install and load the package “tidyverse”. Please copy in the code you used.
a. Report at least one other way of installing a package.

setwd("C:/Users/ellio/OneDrive/Documents/Uni/Yr4 Masters/Block 1/BIM


EBC4280/Wk1/a1")
## replace this with your own pathway

library(tidyverse)
#Alternative method
## Tools >> Install Packages >> "type in packages seperated by commas" >> load
packages

( 73+4 )∗15
2. Compute and assign the name calculation to the result. Print calculation
√ 43
to the console and report the value below. Please copy in the code you used.
a. Now, standardize calculation by subtracting its mean and dividing by its
standard deviation. What is the result and why? Please copy in the code you
used.

[1] 176.1358

calculation <- ((73+4)*15)/sqrt(43)


calculation

a.
[1] NA

ans <- (calculation - mean(calculation))/ sd(calculation)


ans

the result does not have a sd. Only a range of numbers can have a sd. Thus c()

3. Create a vector called “a” that has the numbers 1 to 50. Then create a vector called
“b” that has the numbers 51 to 100. Assign the two vectors to a matrix called m1 that
has 2 columns. Please copy in the code you used.

a <- 1:50
b <- 51:100

m1 <- a+b

4. Create a vector called months containing the numbers 76, 32, 84, and 9. Compute a
vector called years from it by dividing months by 12. Report the value of years below
and copy in the code you used.

[1] 6.333333 2.666667 7.000000 0.750000

months <- c(76,32,84,9)


years <- months / 12
years

Comparisons and logical operators


R, like many other languages uses (logical) operators to perform comparison tests.
Understanding how these operators work is essential to writing code. The operators are:
- > greater
- > = greater or equal
- < less
- < = less or equal
- = = equals
- ! = does not equal
- & AND
- | OR
- There are also if(), else if(), ifelse(), else but let's not get carried away for now.

5. What happens when you check whether a is larger than b? Explain. Note that you
created these vectors above.

1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE


[8] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[15] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[22] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[29] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[36] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[43] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[50] FALSE

Because each vector/number in the sequence of a is less than the same vector in sequence
b.

1<51; 2<52.. etc.

6. Is the mean of a smaller or equal to the mean of b. Please copy in the code you used.
How is this operation different than in question 5?

> mean(a) <= mean(b)


[1] TRUE

mean(a) <= mean(b)

7. Is the vector c(1, “a”, 3) equal to the vector c(1, 2, “3”)? Do you think it makes sense
what R is doing here?

[1] TRUE FALSE TRUE

Yes, R is again comparing individual arguments in the list. As the list is made up of
different kind of values, it can compare numeric and non-numeric values
8. Imagine there is a medical study and patients should be excluded from the study if
they weigh more than 90 kg or if they are younger than 18 years. Define the vector
age as age <- c(50,17,21,16,90) and the vector weight as weight <-
c(80,75,92,106,69). Then write a logical statement involving these two variables
that tests for the exclusion criteria. How many people qualify for the study? Please
copy in the code you used.

[1] 2

age <- c(50,17,21,16,90)


weight <- c(80,75,92,106,69)

criteria <- age >= 18 & weight <= 90


criteria

sum(criteria)

Graphics and data

9. Load the d1.csv dataset into R and object called data1. Which function do you need to
use and why? Report at least one other way on how you could load this data. Please
copy in the code you used.

Data1 <- read.csv("d1.csv")

Click on file in files > import dataset > name dataset > import

10. Create a vector called new that is the result of a product of the variables "ahi01" and
"ahi02" from the data1 dataset. Why is this vector not another variable in the data1
dataset? Please copy in the code you used.

new <- data1$ahi01 * data1$ahi02

because the vector was called “new”


Creating a new variable would require you the insert this new variable into the dataset
using “data1$new”

11. Create a histogram of the elapsed.days variable from the data1 dataset. What type of
distribution is this?
Negative exponential distribution

12. Create a scatter plot between the variables ahiTotal and cesdTotalfrom the data1
dataset. Can you already comment on the direction of the relationship between these
two variables? Hint: you can use plot() or a more complex version from the ggplot2
package.
a. For a bonus, try to give the plot a title and change the x and y coordinate
names.

High negative correlation

plot(data1$ahiTotal, data1$cesdTotal,
main = "The Negative Distribution",
xlab = "X Total",
ylab = "Y Total")

Where are the mistakes:

13. If you run the following code: L1 <- list(a,b,data2) what type of error will you get
and why? How would you solve it?

Error: object 'data2' not found

“” data2 to verify it is a character

14. If you run the following code: c <- c(a, b, 5, 6,7 8, 9) where is the mistake? How
would you solve it?

Error: unexpected numeric constant in "c <- c(a,b,5,6,7 8"

Add comma between 7 and 8

15. Why will the value of d <- a[51] be NA?

[1] NA

A[51] does not exist. A only has values from 1:50. Thus only having 50 vectors

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy