R Programing

R Programing

What's that data type?
100xp
Do you remember that when you added 5 + "six", you got an error due to a mismatch in data types? You can avoid such embarrassing situations by checking the data type of a variable beforehand. You can do this with the class()function, as the code on the right shows.
Instructions
Complete the code in the editor and also print out the classes of my_character and my_logical.
Arithmetic with R
100xp
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponentiation: ^
Modulo: %%
The last two might need some explaining: - The ^ operator raises the number to its left to the power of the number to its right: for example 3^2is 9. - The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or 5 %% 3 is 2.
With this knowledge, follow the instructions below to complete the exercise.
Instructions
Type 2^5 in the editor to calculate 2 to the power 5.
Type 28 %% 6 to calculate 28 modulo 6.
Click 'Submit Answer' and have a look at the R output in the console.
Note how the # symbol is used to add comments on the R code.
Variable assignment
100xp
A basic concept in (statistical) programming is called a variable.
A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable.
You can assign a value 4 to a variable my_var with the command
my_var 5 returns TRUE. The nice thing about R is that you can use these comparison operators also on vectors. For example:
> c(4, 5, 6) > 5
[1] FALSE FALSE TRUE
This command tests for every element of the vector if the condition stated by the comparison operator is TRUE or FALSE.
Instructions
Check which elements in poker_vector are positive (i.e. > 0) and...

Similar Essays