Chapter 3 Getting started
3.1 Basic operations
Here, we will be going over the very basic operations involved in coding. Most of coding relates to mathematics and logic! For example, below is a table that features basic math (addition, subject, etc.), how it is written in R (pay attention to the symbols used), and its result.
Math | R code |
Result |
---|---|---|
\(8 + 4\) | 8 + 4 |
12 |
\(8 - 4\) | 8 - 4 |
4 |
\(8 \cdot4\) | 8 * 4 |
32 |
\(8 / 4\) | 8 / 4 |
2 |
\(8^4\) | 8 ^ 4 |
4096 |
\(\sqrt{100}\) | sqrt(100) |
10 |
3.2 Variable assignment
A variable is a basic concept in programming: it allows you to store a value or an object in R. You can then call this variable’s name to easily access this value or object since it is already stored into R’s memory. For instance, let’s assign the variable X to a value of 1, and Y to a value of 2.
# Assigning a value of 1 to 'X'
<- 1
X # Assigning a value of 2 to 'Y'
<- 2 Y
Notice the use of <-
. You can think of this as an equal sign (=). This is the symbol combination used to assign a value to a variable. Logically, know that the value points to the variables saying “hey, this value is what this varaible should be”.
3.3 Basic data types
There are 5 data types to know of:
- Character: Letters and words. For instance, in research, this could be seen in data as categorical variables like gender identity:
man
. - Logical: These result in two possible values:
TRUE
orFALSE
(orNA
…). - Numeric: This is typically the default when dealing with numbers. These include numbers with decimal points.
1
,1.5
,10.2
. - Integer: These are WHOLE numbers without decimals
1
5
10
. - Complex: This is something we will just gloss over- in sum, an equation with an imaginary number
2 + 5i
.
3.4 Download and complete the homework assignment!
Open this link to download the first homework assignemnt. https://github.com/gavin-mullin/R-Workshop-2024/blob/main/ASSIGNMENTS/assignment1_chapter3.Rmd
This link will send you to Gavin’s github page, specifically to the folder containing the homework. To download the assignemnet, clikc the three dots in the top right hand of your screen and click download. Alternatively, just click ctrl-shift-s
on your keyboard while on the page.