-
Notifications
You must be signed in to change notification settings - Fork 28
/
01_introToR.Rmd
122 lines (76 loc) · 8.19 KB
/
01_introToR.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: "Introduction to the R programming language"
output:
bookdown::html_document2:
includes:
in_header: assets/01_introToR_image.html
after_body: assets/foot.html
---
# Ready to begin?
You’re about to start an adventure to learn R and statistics. If this is your first time working with R, then you should begin on this page. If you’re comfortable with R basics and you’d like to start with the statistical content, please proceed onto the islands with this [link](02_bellCurve.html).
# Using interactive windows
Throughout this material we will use an interactive version of R provided by [learnR](https://rstudio.github.io/learnr/). Although this way of using R comes with some limitations regarding functionality, it will give us the benefit of being able to run R code without switching from a web browser to a standalone application such as [RStudio](https://www.rstudio.com/).
Below you can see an example of an interactive R window. In the white window (under the button with the text *Start Over*) is where you will write code to be executed. To run code you have written, click the green *Run Code* button and observe how information pops up under the window. If no errors were made, this is where the answer will be returned. Error messages are shown in a red box, also under the learnR window.
Now spend a few minutes using the window below as a calculator and run simple calculations by clicking *Run Code*.
<!---LEARNR EX 1-->
<iframe class="interactive" id="myIframe1" src="https://tinystats.shinyapps.io/01-intro-ex1/" scrolling="no" frameborder="no"></iframe>
<!------------->
# Objects in R
R is an object oriented programming language, meaning when working in R we will create different types of objects and use operators and functions to manipulate these objects.
To create a new object in R we first pick a name for the object and tell R what to assign to it using **assignment operators**; either `=` or a small arrow `<-` made from the combination of `<` and `-`.
* Use the window below to assign numbers to objects and output the content by typing out the name of the object.
* After creating two numerical objects, use the mathematical operator `+` to add these objects together and see what happens when you run the code.
<!---LEARNR EX 2-->
<iframe class="interactive" id="myIframe2" src="https://tinystats.shinyapps.io/01-intro-ex2/" scrolling="no" frameborder="no"></iframe>
<!------------->
# Common functions
Most of the time when we work in R, we will use functions; either pre-written functions or ones we write ourselves. **Functions** make it easy to use sets of code instructions repeatedly (without filling our scripts with the code underlying the function) and help us carry out multiple tasks in a single step without having to go through the details of how the steps are executed.
To use functions in R, we type the name of the function followed by parentheses (e.g. `print( )`). Within the parentheses is where we will specify the function input and options, separated by commas `,`. One function you will use a lot is the **combine function** `c( )`, which as the name suggests lets you concatenate different types of values.
* In the window below, create an object combining a set of numerical values using `c( )`, separating the different values with commas.
* Then sum up the content of this object using `sum( )`.
<!---LEARNR EX 3-->
<iframe class="interactive" id="myIframe3" src="https://tinystats.shinyapps.io/01-intro-ex3/" scrolling="no" frameborder="no"></iframe>
<!------------->
# Writing your own function
R makes it easy to create user defined functions by using `function( )`. Here is how it works:
* Give your function an object name and assign the function to it, e.g. `my_function_name <- function( )`.
* Within the parentheses you specify inputs and options just like how pre-written functions work, e.g. `function(input_data)`
* Next, put all the code you want your function to execute inside curly brackets like this: `function(input_data){code to run}`
* Use `return( )` to specify what you want to your function to output once it is done running the code.
Use the following instructions to complete the function in the window below:
* We've started writing a function for you that will sum up values and take the square root of the sum. To take the square root, we use the `sqrt( )` function.
* Complete this function by filling in `input_data` for the sum( ), and then filling in the remaining empty parentheses with the appropriate object names.
* Now create an object containing a set of numerical values and call it `my_combined_values`. Then try out your new function on this object, which will compute the square root of the object's sum.
<!---LEARNR EX 4-->
<iframe class="interactive" id="myIframe4" src="https://tinystats.shinyapps.io/01-intro-ex4/" scrolling="no" frameborder="no"></iframe>
<!------------->
# Functions within functions
It is also possible (and sometimes very useful) to put a function within another function. For example, we could combine multiple steps into one like this: `sqrt(prod(c(1,2)))`, resulting in one line of code that both generates the values and directly calculates the square root of the product of those values.
# Vectors
The `c( )` function will combine values and create a specific type of data structure called an **atomic vector**. A vector is a simple one-dimensional structure that can contain only one type of value. This means that storing multiple numeric values in a vector, as we have already done, works just fine. But there are other types of values that can be used in R, for example **character** values. These values are created by putting text or numbers within quotes such as `"Giraffe"`. If we try to combine numeric and character values in the same vector, R will convert both values to the character type.
```{r}
c("Giraffe", 123)
```
This behavior is not always desirable, so it is a good idea to try to only combine values of the same type.
# Boolean values and logical operators
Another type of variable in R is the boolean (TRUE/FALSE) type. **Boolean** or logical vectors can only take two different values; TRUE or FALSE (case sensitive). You will see these types of values mostly when logical operators are used to test how different objects relate to each other. For example, the **logical operator** `==` can be used to test if two different objects are exactly the same and `TRUE` will be returned only if the identity is fulfilled.
```{r}
"Giraffe" == "Teacup"
sqrt(100) == 10
```
# Data frames
Vectors are one of multiple data structures in R. We will not cover all types of structures here, but perhaps the most common one encountered when analyzing data in R is the **data frame**. Data frames are two dimensional, which basically means that data frames let you store multiple vectors in one object. Oftentimes, each vector will be a new column in the data frame. One constraint though is that all vectors/columns need to be of the same length.
* In the window below, use the `data.frame()` function to create a data frame with two columns called *x* and *y*. When creating data frames we specify a column by giving the column a name and assigning values or vectors to it, e.g. `data.frame(x= c("Giraffe", "Teacup"))`.
* Also observe what happens when you try to combine an x and y vector of different lengths.
<!---LEARNR EX 5-->
<iframe class="interactive" id="myIframe5" src="https://tinystats.shinyapps.io/01-intro-ex5/" scrolling="no" frameborder="no"></iframe>
<!------------->
As you could see above, running a line of code with just the name of a data structure (in this case, the letter *d*) will print the full data frame in the console output (if no errors were made!). If you instead are interested in only one of the columns, this can be specified by using the `$` operator followed by the column's name, e.g `d$x`. Try it out below!
<!---LEARNR EX 6-->
<iframe class="interactive" id="myIframe6" src="https://tinystats.shinyapps.io/01-intro-ex6/" scrolling="no" frameborder="no"></iframe>
<!------------->
# You're ready to go!
This was a brief introduction into R, but now you know what you need to get started with the [first module](02_bellCurve.html)!
<script>
iFrameResize({}, ".interactive");
</script>