# Assginment lecture 1: hand in before Sunday October, 2nd, 18:00
# Please paste your answer between the lines #=======
# Please read the questions and answer descriptions well
# All you write should be running code. Otherwise, comment out with #
# Save your file as "yourname_assignment1.R" and send it to
# gilles.dutilh@unibas.ch
## (1)
# type the following 5 lines (without the #-symbols, these tell R NOT to execute the line):
# a = b = c = 1
# b = 3
# 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?
# =========================================================================================
# =========================================================================================
## (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)
# =========================================================================================
# =========================================================================================
## (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?)
# =========================================================================================
# =========================================================================================
## (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)
# =========================================================================================
# =========================================================================================
## (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?
# =========================================================================================
# =========================================================================================
## (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)
# =========================================================================================
# =========================================================================================
## (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 :)
# =========================================================================================
# =========================================================================================
## (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.
# =========================================================================================
# =========================================================================================
## (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.)
# =========================================================================================
# =========================================================================================