Welcome to your first training session!
No need to be nervous: this page contains not only tasks, but also their solutions as folded code elements. You can unfold these code blocks by simply clicking on them. Give it a try:
Click here to see more
# Well done!
# Spoiler! - you will find your answer here
- Create a variable called
a
and assign the number 2017 to it!
Click here to see the answer
# use the "<-" operator for variable assignments:
<- 2017 a
- Calculate the square root of 1089 and save the result in variable
b
!
Click here to see the answer
# use the built- in function "sqrt()" and the number 1089 as an argument:
<- sqrt(1089) b
- Calculate the sum of
a
andb
!
Click here to see the answer
# done via standard operators:
+ b
a ## [1] 2050
- Overwrite variable
a
by assigning the value 2018 to it!
Click here to see the answer
# simply assign a new value to an existing variable in order to overwrite it
<- 2018 a
- Make a copy of variable
b
and name itc
!
Click here to see the answer
# variable assignment works from right (existing variable) to left (new variable):
<- b c
- Create your own function called
my.fun()
, which requires three variables as input. The function should generate the square root of the product of all three variables and return one numeric value!
Click here to see the answer
<- function( var1, var2, var3 ) {
my.fun <- sqrt( var1 * var2 * var3 )
result return(result)
}
- Use
a
,b
andc
(from the previous tasks) as input intomy.fun()
and save the output to variabled
! Check the resulting value!
Click here to see the answer
<- my.fun(a, b, c)
d
d## [1] 1482.431