Answers to Week 1 Exercises PDF

Title Answers to Week 1 Exercises
Course The Psychopathology of Everyday Life
Institution University of Melbourne
Pages 5
File Size 83.3 KB
File Type PDF
Total Downloads 18
Total Views 194

Summary

Questions and answers to problems presented in Week 1 lectures....


Description

Week 1 RHMI Exercise Answers Andrew Perfors! Day 2 Operators 1. One mile is 1.61 kilometres. Use R to figure out how many kilometres 5 miles are. > 5*1.61 [1] 8.05 5 miles is equal to 8.05 kilometres.!

2. Use R to calculate (8+4)*12. How is that different from 8+4*12? > (8+4)*12 [1] 144 > 8+4*12 [1] 56 These are different because the parentheses mean the order of operations is different.!

3. TRUE or FALSE?

eight is less than six

> 8 < 6 [1] FALSE Eight is not less than six!!!

4. TRUE OR FALSE? 52 is equal to 81? > 5^2==81 [1] FALSE Yep, 52 is not equal to 81. Note by the way that we had to use the double equal sign: ==. If we’d tried it with a single = we would have gotten an error because it uses a single equal to assign a variable and we do not want that!! > 5^2=81 Error in 5^2 = 81 : invalid (do_set) left-hand side to assignment

5. TRUE OR FALSE? (4 is greater than or equal to 22) AND (1 is less than -1 times -1) > (4 >= 22) & (1 < (-1)*(-1)) [1] FALSE Notice how I used parentheses to make sure that R got the order of operations right. It also makes it more readable. When in doubt always include even more parentheses than you think you might need; it’ll save you trouble later.!

Functions 1. Calculate the square root of 81. > sqrt(81) [1] 9 The square root of 81 is 9!!

2. What happens if you try to take the square root of -81? Use the abs() function to make it nonnegative first. > sqrt(-81) [1] NaN Warning message: In sqrt(-81) : NaNs produced You can’t take the square root of a negative number, so it gives you a warning and returns NaN (which stands for “Not a Number” and is different from NA, which means Not Available. NA means you have something missing from your data, while NaN indicates you’ve tried to do something that is not well-defined mathematically. You can fix it by taking the absolute value first with the abs() function, like below: since functions are evaluated from the outside in, the abs() is evaluated first and then that result (which is just 81) is given to sqrt(), which is fine.! > sqrt(abs(-81)) [1] 9

3. What is the exponent of the log of 2? > exp(log(2)) [1] 2 Those of you who remember your exponents and logarithms from high school may remember that they are just opposites of each other! So taking the exponent of a log of a number just gives you that number… super useful, huh?!

4. Round 4328.29874 to two digits after the decimal place. How can you make it round it to 4000? > round(4328.29874,digits=2) [1] 4328.3 Note that this is still rounded to two, because by convention it doesn’t print the trailing zero. To round to 4000 you might have had to think a little bit and experiment around. The main insight is that if a positive digits rounds after the decimal point, then a negative one must round before it:! > round(4328.29874,digits=-3) [1] 4000

5. See if you can figure out how the floor() function is different from round(). Hint: just experiment around by giving it different values, or use the help() function! Essentially, floor() always rounds down while round() will round to the closest integer, either up or down (for values at 0.5 it defaults to up). We can see this by comparing different numbers:! > floor(3.9) [1] 3 > round(3.9) [1] 4 > floor(3.1) [1] 3 > round(3.1) [1] 3

Variables 1. Make a variable called name with your first name in it. Now make a variable called name with your last name. What has happened? How do you make one with your complete name? > name name [1] "Andy" > name name [1] "Perfors" When I gave it the new value for name, it overwrote the old one! To make one with your complete name you can just do it directly, since character variables can include spaces:! > name name [1] "Andy Perfors"

2. Make a variable called x and set it equal to 2. Then set it equal to itself plus 2. What is it now? > x x x [1] 4 This was fairly straightforward, the main point was to realise that you have to assign the new value to x. If you just do the below, it will add two to it but x will remain the same:! > x x+2 [1] 4 > x [1] 2

3. Make a variable called y and set it to TRUE. Then add three to it. What happens? How is this different if you set it to FALSE instead? What do you think is going on? > y > y > y [1] > y > y > y [1]

90 part in square brackets, as that is what tells R to return those values. If I hadn’t I would have just gotten a vector of TRUE and FALSE corresponding to each item in ages, TRUE if that item was greater than 90 and FALSE if it wasn’t:! > ages>90 [1] TRUE

TRUE FALSE FALSE FALSE FALSE

Since I have people older than 90 let’s try it with a threshold of 100:! > ages[ages>100] numeric(0) This indicates that it’s returning a numeric vector of length 0, i.e., there is nobody that is older than 100 in my vector. To do it with the value of 20 I do the following:!

> ages[ages>20] [1] 93 98 71 35 38 This returns all the ages values over 20, which is almost everyone.!

3. Have R return the names of the people in your family who are older than 20. > family[ages>20] [1] "Elizabeth" "Philip"

"Charles"

"Harry"

"Meghan"

You can see this is very similar to the above but I’m selecting the values out of the family vector instead of the ages one. The ages>20 returns a vector of TRUE and FALSE based on whether the age is greater than 20. To get the corresponding names, I need to put that in brackets inside family (which is the list of names) and it will list the ones for which the value is TRUE.!...


Similar Free PDFs