Skip to content

Commit

Permalink
progress up to 4/3/24
Browse files Browse the repository at this point in the history
  • Loading branch information
wokech committed Mar 5, 2024
1 parent 8a12866 commit 3d86a2e
Show file tree
Hide file tree
Showing 12 changed files with 2,030 additions and 42 deletions.
212 changes: 207 additions & 5 deletions arithmetic_variables.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,221 @@

## Lesson Content

*In progress*
*In progress (4/3/24)*

### Basic Arithmetic

To get started, first, we will open R or RStudio. In R, go to the console, and in RStudio, head to the console pane. Next, type in a basic arithmetic calculation such as ```1 + 1``` after the angle bracket (```>```) and hit “Enter.”

**NOTE**

Code Style – have a space on either side of the operator (```a <- 2``` / ```b <- 4 + c```)

```{r}
1 + 1
```

The output will be observed next to the square bracket containing the number 1 ([1]). The 1 indicates that the indexing begins at position 1. Here, as opposed to Python, which uses zero-based indexing, R uses one-based indexing.

*Figure*

Additionally, to include comments into the code block, we use the hash (#) symbol. Anything written after the code block will be commented out and not run.

```{r}
# A simple arithmetic calculation (which is not run because of the hash symbol)
1 + 1
```

### Arithmetic operators

Various arithmetic operators (listed below) can be used in R/RStudio.

| Arithmetic Operator | Description |
|:-------------------:|:----------------------------------:|
| \+ | Addition |
| \- | Subtraction |
| \* | Multiplication |
| / | Division |
| \*\* or \^ | Exponentiation |
| %% | Modulus (remainder after division) |
| %/% | Integer division |

Examples

Create an R script called ```my_arithmetic_1.R```

Run the following calculations in the script and save the file to the working directory.

#### Addition

```{r}
10 + 30
```

#### Subtraction

```{r}
30 - 24
```

#### Multiplication

```{r}
20 * 4
```

#### Division

```{r}
93 / 4
```

#### Exponentiation

```{r}
3^6
```

#### Modulus (remainder with division)

```{r}
94 %% 5
```

#### Integer Division

```{r}
54 %/% 7
```

#### Slightly more complex arithmetic operations

```{r}
5 - 1 + (4 * 3) / 16 * 3
```

### Variables

Variables are instrumental in programming because they are used as “containers” to store data values. To assign a value to a variable, we can use <− or =. However, most R users prefer to use <−.

- Variables store values for later use in your code, improving readability and efficiency.
- Assign values to variables using the <- operator (e.g., x <- 5).
- Variable names should be descriptive and avoid special characters.

#### Variable assignment

1. Using `<-`

```{r}
variable_1 <- 5
variable_1
```

2. Using `=`

```{r}
variable_2 <- 10
variable_2
```

3. Reverse the value and variable with `->`

```{r}
15 -> variable_3
variable_3
```

4. Assign two variables to one value

```{r}
variable_4 <- variable_5 <- 30
variable_4
variable_5
```

The output of the variable can then be obtained by:
1. Typing the variable name and then pressing “Enter,”
2. Typing “print” with the variable name in brackets, print(variable), and
3. Typing “View” with the variable name in brackets, View(variable).

Both ```print()``` and ```View()``` are some of the many built-in functions available in R.

*Figure*

```{r}
print(variable_1)
```

```{r}
View(variable_2)
```

Output of ```View()``` will be seen in the script pane.

### The ```assign()``` and ```rm()``` functions

In addition to using the assignment operators (<- and =), we can use the assign() function to assign a value to a variable.

```{r}
assign("variable_6", 555)
variable_6
```

To remove the assignment of the value to the variable, either delete the variable in the “environment pane” or use the rm() function.

```{r}
variable_7 <- 159
```

```{r}
rm(variable_7)
```

After running rm() look at the environment pane to confirm whether variable_7 has been removed.

#### Naming variables
At this point, you may be wondering what conventions are used for naming variables. First, variables need to have meaningful names such as current_temp, time_24_hr, or weight_lbs. However, we need to be mindful of the variable style guide (link) which provides us with the appropriate rules for naming variables.

object_name <- value

Objects must start with a letter and only contain letters and numbers

Some rules to keep in mind are:
1. R is case-sensitive (variable is not the same as Variable),
2. Names similar to typical outputs or functions (TRUE, FALSE, if, or else) cannot be used,
3. Appropriate variable names can contain letters, numbers, dots, and underscores. However, you cannot start with an underscore, number, or dot followed by a number.

#### Valid and invalid variable names

Types of variable names:

- snake_case
- CamelCase
- use.periods
- snake.case.CamelCase_snake_case

Valid names:
- time_24_hr
- .time24_hr

Invalid names:
- _24_hr.time
- 24_hr_time
- .24_hr_time

## Exercises

i. In R, calculate 3 + 5 and then 4 \* 6.
ii. Assign the value 10 to a variable called x. Then calculate x \^ 2. Next, calculate the expression (3 + x) \* (x - 1) using the x from before.
i. In R, calculate ```3 + 5``` and then ```4 * 6```.
ii. Assign the value 10 to a variable called x. Then calculate ```x ^ 2```. Next, calculate the expression ```(3 + x) * (x - 1)``` using the x from before.
iii. Declare a variable to store your age and assign a value to it. Print the variable value.
iv. Create variables for the length and width of a rectangle and calculate its area.
v. Explain the rules for naming variables in R. Provide examples of valid and invalid variable names.
vi. What is operator precedence in R? How does it work for arithmetic operators?
vii. What data types can be used for arithmetic operations in R?
viii. What is the difference between \<- and = for assignment in R?
viii. What is the difference between ```<-``` and ```=``` for assignment in R?

## Summary

In this chapter, I have demonstrated how to perform basic arithmetic operations and how to use the various arithmetic operators available in R/RStudio. Additionally, the concept of variables and values has been explained, and conventions for appropriately naming variables have been discussed. In the next chapter, we will look at the other types of primary operators in R/RStudio.
In this chapter, I have demonstrated how to perform basic arithmetic operations and how to use the various arithmetic operators available in R/RStudio. Additionally, the concept of variables and values has been explained, and conventions for appropriately naming variables have been discussed. In the next chapter, we will look at the other types of primary operators in R/RStudio.
Loading

0 comments on commit 3d86a2e

Please sign in to comment.