For a warm up, this worksheet handles writing and executing R scripts from within R studio. After completing this worksheet you should be able to write simple R scripts in R studio and execute individual lines.
R Studio — we recommend to use R Studio for (interactive) programming with R. You can download R Studio from the official web page.
In this first worksheet, we will do some finger exercises in R using the R Studio IDE.
Please open R Studio, write a code line into a new R script which will print “Hello World” and execute this single line using the <Ctrl>-<Return> keyboard shortcut.
If you have performed this task: Welcome to R! Now you are up to more and we will use R as a pocket calculator.
Please try to compute the result of at least five mathematical expressions using only the standard arithmetic operators.
As you noticed, R computes the expressions and returns you the result on the console. If you want to store the values you have used as well as the results, you have to use variables.
Please repeat your calculations but this time store the values and equations in different variables and only print the results on the console after all five computation have been finished.
So far, you have only used individual values within each equation. What happens if you e.g. use vectors with a length larger than one? Let's try it.
Please create two variables containing vectors, each of length five using the c() function and try to get the sum of each value pairs (i.e. the sum of the first entries in each vector, the sum of the second etc.) as well as the squared sum of them.
Finally, it is time for some more advanced math stuff.
Please compute the mean of each of your two vectors using built in R functions in at least two different ways. If you can not guess the names of the functions: internet search engines are your friend.
?<function name without brackets>
into your R console. If you look for something unspecific, type
“??<search key word>”