##########################################
##### first Lecture R-course HS-2016 #####
##########################################
# R is a calculator:
1 + 1
2 * 3
2 + 1 / 3 # R knows the order of operations
(2 + 1) / 3
3 ^ 2 / 2 + 1 # hey, nothing happens
# what is behind a hash (#) is not read by R, thus useful
# for commenting in your code
# R is calcultor... that can use variables:
A = 2 # assignment of value 2 to variable a
print(A)
A
B = A + 2 # calculate with variables using arithmetic operators
B
C = B * A
C
# also, you might sometimes read:
C <- B * A
# which is better for some principled reasons
# I however prefer to use =
D = "hello world"
D + 1 # error, you cannot add a number to a word!
# So, we create some different variables (A - D).
# And for now, they are
# remembered by R in what we call the workspace.
# This is just the temporal memory of R, which can be
# called by ls()
ls() # shows the variables you created.
# To variables, we can apply functions
sqrt(B) # the function sqrt() calculates the square root,
# in this case of variable B
B ^ (1/2) # is, following basic algebra, the same
sqrt(B)*sqrt(B) # what a surprise
B # and B is still B until we will overwrite it below
B = 'now I became a character string'
# So, a variable is a word you choose
# (at least beginning with a letter) to which you assign
# a value (e.g., a number or a character string)
# Almost any word can be used as variable name
my.name = "gilles"
your_age = 22
# variable names are case sensitive, so
my.Name # doesn't exist
# Aritmic operators (+, -, *, /, (), ^) work as you
# are used to.
# Functions always have brackets -> sqrt(x) and are
# applied to the variables within the bracket. A function
# always has one or more specific variable type(s)
# that it applies to. for example:
sqrt(A) # the root of 2
sqrt(B) # cannot take the root of a letter string
nchar(B) # how many letters in the string?
nchar(A) # hey, this works! R is smart enough to understand that
# you probably meant nchar of the string a ("2")
# does this work the other way around?
my.new.variable = "4"
sqrt(my.new.variable) # no, these interpretations go only in one direction.
# Now: VECTORS
# vectors are the most basic form of information in R
# in fact the variables above are vectors with length 1!
# the c() function binds the values you give together in a vector
# say, we have exam scores of different participants:
scores.test1 = c(28, 35, 43, 30, 52, 20, 101) # create a vector with numbers
scores.test2 = c(5, 6, 2, 4, 5, 3, 4) # and another one
scores.test1
scores.test2
# lets add the two scores:
# try:
scores.test1 + scores.test2
# assign to variable
sum.of.scores = scores.test1 + scores.test2
# maybe, you want to increase all scores on test 1,
# because there was one question too difficult:
# try:
scores.test1 + 10
new.scores.test1 = scores.test1 + 10
# maybe, we want to weight the sum a bit, because the scale of test 1 is
# very different than the scale of test 2
# try:
10 * scores.test2
new.scores.test2 = 10 * scores.test2
weigthed.sum.of.scores = new.scores.test1 + new.scores.test2
# lets try multiplication:
scores.test1 * scores.test2
# these vector artihmetic can be really useful:
# my experiment program gave me -1 and 1 as responses
# but I want them to be 0 and 1
resp = c(-1, 1, 1, -1, 1, 1, -1)
resp01 = (resp + 1)/2
# the c() function also binds vectors together
c(scores.test1, scores.test2)
# So, also vectors of length 1:
c(scores.test1, 999, scores.test2)
# when making a vector of different variable types,
# R converts "up in the type hierarchy"
my.vector = c('hello world', 9.0, 10.5)
my.vector2 = c(0, 1, 3.3, 5)
is(my.vector)
is(my.vector2)
# some important calculations:
mean(resp01)
sd(resp01)
var(resp01)
cor(resp01, resp) # what a surprise
sum(scores.test1)
max(scores.test1)
min(scores.test1)
sort(scores.test1)
order(scores.test1)
# ask some very basic, but important questions about these variables
3 < 4
C < A
C > 10
new.scores.test2 > new.scores.test1 # bigger than?
new.scores.test2 < new.scores.test1 # smaller than?
new.scores.test2 >= new.scores.test1 # bigger than or equal to?
new.scores.test2 <= new.scores.test1 # smaller than or equal to?
2 == 3 # are 2 and 3 equal?
scores.test2 == 5 # are elements in vector a equal?
new.scores.test2 == new.scores.test1
new.scores.test2 != new.scores.test1 # and the reverse: are they different?
# these operators are called "binary operators"
# You can assign these true's and falses (which form a vector of logicals)
# also to a variable:
isasmallerthanc = a < c
##