## Solutions for the assignments of lecture 1
## (1)
# type the following 5 lines (without the #-symbols, these tell R NOT to execute the line):
# a = b = c = 1
# b = 2
# c = c + 2
# rm(a)
# ls()
# to understand the rm(a) means, type ?rm
# What happened in these 5 lines of code? What variables have we left now and what are
# their values?
# =========================================================================================
# a, b, c became 1; then b became 3; then c became 3; then a was removed and we were left
# with b = 3 and c = 3
# =========================================================================================
## (2)
# Make a variable month.lengths that contains the number of days in each month of the year,
# so, 31, 28, 31, 30 etc). What is the mean number of days in a month? (use the mean
# function for that)
# =========================================================================================
month.lengths = c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
mean(month.lengths) # 30.42
# =========================================================================================
## (3)
# make the vector b = c(1, 3, 2, 5, 4, 3, 6, 5)
# type: b > 3
# You get a couple of TRUE's and FALSE's. To what question are these TRUE's and FALSE's
# the answer? (what do they say?)
# =========================================================================================
b > 3
# The TRUE's and FALSE's are the answer to: "true or false: b is bigger than 3?"
# This question is answered for each element of b.
# =========================================================================================
## (4)
# Above, you created vector b.
# Qustion 4 A)
# Use this vector and arithmetic to create a new vector b.plus.one which looks like:
# c(2, 4, 3, 6, 5, 4, 7, 6)
# Question 4 B)
# Now use similar arithmetic to create another vector, say b2, that contains:
# c(20, 40, 30, 60, 50, 40, 70, 60)
# =========================================================================================
# A)
b.plus.one = b + 1
# B)
b2 = b.plus.one * 10
# or
b2 = (b + 1) * 10
# =========================================================================================
## (5)
# At question 3, you typed b > 3. This gave you a vector of TRUE's and FALSE's. You can save this
# vector by typing, e.g., bigger.than.3 = b > 3.
# Apply the function which() to this bigger.than.3 variable. What does this tell you?
# =========================================================================================
which(bigger.than.3) # gives you the indices where this variable is TRUE. In other words,
# it tells you which values of b are bigger than 3.
# =========================================================================================
## (6)
# make the vector d that contains 4 values: 1, 0, 1, 0
# now type:
# all(d)
# sum(d)
# try to understand the result
# Which of these two applications of a function to variable d needed some transformation?
# (you got a warning about that)
# =========================================================================================
# all(d) is a function for logical vectors, so R automatically transforms 1's and 0's
# to TRUE's and FALSE's
# sum(d) is a numerical operation and can be perfectly applied to 0's and 1's
# =========================================================================================
## (7)
# A related question: make the vector
# some.numbers = c(3, 2.5, 2, 1.5, 1, .5, 0, -.5, -1, -1.5, -2, -2.5, -3)
# now type: sum(some.numbers < 0)
# what do you get?
# R has summed the result of some.numbers < 0, which is a vector of TRUE's and FALSE's.
# Try: some.numbers < 0
# Then, to sum this, R first tranformed each TRUE to 1 and FALSE to 0.
# This might seem tricky, but it will later on turn out to be really convenient.
# This is not really a question, so no need for an answer :)
# =========================================================================================
# if you were creative, you could have made some.numbers by:
some.numbers = 6:-6/2
# =========================================================================================
## (8) CHALLENGE QUESTION
# checking whether a number can be devided by, say, 4 can be done with:
# number%%4 == 0 (try this and of course, replace 'number' by any number you like)
# 4%%2 for example substracts 2 from 4 until no more two can be substracted and returns
# what is left. If the number is even, this is zero (4 - 2 - 2 = 0). Therefor, the trick
# above checks if a number is even.
# Now: use variable b that you created above (or need to create again now) to produce a
# vector that says TRUE for all values in b that are even and false for all values that
# are odd.
# =========================================================================================
b %% 2 == 0
# =========================================================================================
## (9) CHALLENGE QUESTION 2
# Not all characters can be used as names of variables in R.
# For example, try: 3numbers = c(44, 22, 33) This will give an error.
# Play around a bit and find which other symbols are not allowed. (you could also ask the
# internet for a complete solution.)
# =========================================================================================
# "forbidden" symbols in object names include all operators: +-*/=<>=!&|~$:
# So not: fives+fours = c(5, 4, 4, 5, 4, 5, 4, 4 ,5)
# An object name cannot start with a number.
# So not 3numbers = c(44, 22, 33)
# An Object cannot take any of the "reserve" words in R (see ?reserved), which include:
# TRUE, FALSE, in, else, function
# =========================================================================================