Skip to content

Commit

Permalink
updates by march 10th
Browse files Browse the repository at this point in the history
  • Loading branch information
wokech committed Mar 10, 2024
1 parent 2bebf8b commit 1d481d7
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 125 deletions.
4 changes: 2 additions & 2 deletions appendix.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ To further your R learning journey, I would recommend a review of the following
- [R-Ladies Global](https://rladies.org/)
- [satRday](https://satrdays.org/)
- [rOpenSci](https://ropensci.org/community/)
- **Twitter (now X) – #rstats and #rstudio**
- **Reddit – r/rstats and r/RStudio**
- **Twitter (now X)** – #rstats and #rstudio
- **Reddit** – r/rstats and r/RStudio

## 3. Conferences and Meetups

Expand Down
48 changes: 26 additions & 22 deletions arithmetic_variables.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@

To get started, first, we will open R or RStudio (@fig-arithmetic-1). 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
**NOTE**: According to the Tidyverse Style Guide there should be a space on either side of the operator.

Examples

Expand All @@ -45,11 +43,11 @@ Examples
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.
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.

![A basic arithmetic calculation in the R Console](images/arithmetic/arithmetic_console.png){#fig-arithmetic-1}

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.
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}
Expand All @@ -64,17 +62,17 @@ 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 |
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `**` or `^` | Exponentiation |
| `%%` | Modulus (remainder after division) |
| `%/%` | Integer division |

Examples
Example

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

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

Expand Down Expand Up @@ -128,10 +126,10 @@ Run the following calculations in the script and save the file to the working di

### 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 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).
- Variables store values for later use in your code. This results in improved 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
Expand Down Expand Up @@ -165,7 +163,13 @@ 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).
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.

Expand All @@ -181,16 +185,16 @@ View(variable_2)

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

### The `assign()` and `rm()` functions
#### 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.
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.
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
Expand All @@ -200,7 +204,7 @@ variable_7 <- 159
rm(variable_7)
```

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

#### Naming variables

Expand Down
44 changes: 11 additions & 33 deletions data_struct_1.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ length(list_5)
Flatten out into a single vector using unlist()

```{r}
unlist(list_5)
```

### Dataframes
Expand All @@ -144,7 +142,7 @@ A data frame is one of the most common data objects used to store tabular data i
- Various data can be stored (such as numeric, factor, and character), and,
- The individual columns should contain the same number of data items.

**NOTE** Tibbles and their advantages
**NOTE**: Tibbles are the modern versions of dataframes, that have improved printing and subsetting features. The tibble package is a main component of the tidyverse; however, a review of the tidyverse is beyond the scope of this book and will be discussed in subsequent lessons.

#### Creation of data frames

Expand All @@ -158,13 +156,11 @@ age <- c(25, 36, 47)
df_1 <- data.frame(level, language, age)
```

#### Other methods to create dataframes

stack() / unstack() / cbind() / rbind()

as.data.frame()
Use `stringsAsFactors = FALSE` to prevent R from converting string columns to factors.

When you use t() on a dataframe, check using class() to ensure that you didn’t convert to a matrix To transpose a dataframe use t() and then as.data.frame()
```{r}
df_2 <- data.frame(level, language, age, stringsAsFactors = FALSE)
```

#### Functions used to manipulate data frames

Expand Down Expand Up @@ -230,7 +226,7 @@ df_1[3,2]
df_1[2, 1:2]
```

j. Access columns with index / subset
j. Access columns with index

```{r}
df_1[, 3]
Expand All @@ -243,39 +239,21 @@ df_1[, c("language")]
k. Access rows with index

```{r}
df_1[2, ]
df_1[2, ]
```

Other methods for accessing

df\[c(1:3), \] and df\$col_name and df\[, “\_\] and df\[ , c(“a”, “b”)\]

\[ = many elements \[\[ and \$ = single element
**NOTE**: To view the internal structure of various data types described above, the learner can use the `str()` function.

df\[row_indices, column_indices\]

df\[row_indices, \]

df\[ , column_indices\]

which\[ry\$a \> 1\] returns the row indexes of the column “a” that has a value \> 1
Example

```{r}
ry$a = ry[ , 1]
str(list_3)
```

attach()

str() to explain the output

If you have 4 columns

```{r}
df[ , c(3,4)] = df[ , -c(1,2)]
str(df_1)
```

glimpse() / View() / unique() / distinct() complete.cases() - eliminate missing values from a vector, matrix, or data frame

## Exercises

i. Explain the characteristics and use cases of lists in R.
Expand Down
27 changes: 21 additions & 6 deletions data_struct_2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ A matrix is a rectangular two-dimensional (2D) homogeneous data set containing r

a. Creation of matrices

Format:
Using the `matrix()` function

`matrix(range, nrow = _, ncol = _)`
Format: `matrix(range, nrow = _, ncol = _)`

```{r}
m1 <- matrix(1:9, nrow = 3, ncol = 3)
Expand All @@ -67,6 +67,12 @@ m2
m3
```

Using `cbind()` and `rbind()`

```{r}
```

b. Obtain the dimensions of the matrices `m1` and `m3`

```{r}
Expand Down Expand Up @@ -252,12 +258,13 @@ arr_1[, , 1]

Factors are used to store integers or strings which are categorical. They categorize data and store the data in different levels. This form of data storage is useful for statistical modelling. Examples include TRUE or FALSE and male or female. Useful for handling qualitative datasets.

**NOTE**: R has a more efficient method for handling categorical variables using the `forcats`package. This will be discussed in a subsequent series focusing on the `tidyverse`.

```{r}
vector <- c("Male", "Female")
```

```{r}
factor_1 <- factor(vector)
factor_1
```
Expand All @@ -273,13 +280,21 @@ factor_2
as.numeric(factor_2)
```

**NOTE**: To view the internal structure of various data types described above, the learner can use the `str()` function.

Example

```{r}
droplevels()
str(m5)
```

Create example with the following sequence: - as.numeric() - as.character() - factor()
```{r}
str(arr_1)
```

*NOTE: R has a more efficient method for handling categorical variables using the `forcats`package. This will be discussed in a subsequent series on the `tidyverse`.*
```{r}
str(factor_1)
```

## Exercises

Expand Down
10 changes: 5 additions & 5 deletions data_types.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

- Learn how to convert between different data types.

- Use functions like class(), typeof(), and str() to inspect types.
- Use functions like `class()`, `typeof()`, and `str()` to inspect types.

- Convert between data types using the as.\* functions.
- Convert between data types using the `as.___()` functions.

## Lesson Content

Expand All @@ -35,14 +35,14 @@ R and RStudio utilize multiple data types to store different kinds of data.
The most common data types in R are listed below.

| **Data Type** | **Description** |
|-------------------|-----------------------------------------------------|
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Numeric | The most common data type. The values can be numbers or decimals (all real numbers). |
| Integer | Special case of numeric data without decimals. |
| Logical | Boolean data type with only 2 values (`TRUE` or `FALSE`). |
| Complex | Specifies imaginary values in R. |
| Character | Assigns a character or string to a variable. The character variables are enclosed in single quotes ('character') while the string variables are enclosed in double quotes ("string"). |
| Factor | Special type of character variable that represents a categorical such as gender. |
| Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (charToRaw() or rawToChar()). |
| Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (`charToRaw()` or `rawToChar()`). |
| Dates | Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970. |

### Examples of data types in R
Expand Down Expand Up @@ -263,7 +263,7 @@ Character `>` Numeric `>` Integer `>` Logical

- How can you check the data type of a variable in R?

- Use the ```class()```, ```typeof()```, and ```str()``` functions to check the data type of objects.
- Use the `class()`, `typeof()`, and `str()` functions to check the data type of objects.

- Describe the process of coercion in R.

Expand Down
2 changes: 1 addition & 1 deletion download_install.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Once installed, verify that the RStudio software has a similar graphical user in

### Create an RStudio Cloud Account on Posit website

Posit (formerly RStudio) Cloud lets the user access the RStudio interface from their internet browsers (Figure …). Using this option does not require any installation or specific software configuration to be implemented. Posit Cloud offers a [free plan](https://posit.cloud/plans/free) for casual users (without the need for a paid plan) and there is no need for dedicated hardware. Additionally, Posit provides a comprehensive [guide](https://posit.cloud/learn/guide) for first-time users.
Posit (formerly RStudio) Cloud lets the user access the RStudio interface from their internet browsers (@fig-r-cloud-1). Using this option does not require any installation or specific software configuration to be implemented. Posit Cloud offers a [free plan](https://posit.cloud/plans/free) for casual users (without the need for a paid plan) and there is no need for dedicated hardware. Additionally, Posit provides a comprehensive [guide](https://posit.cloud/learn/guide) for first-time users.

#### Step 1

Expand Down
18 changes: 14 additions & 4 deletions importing.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ A dataset is a collection of data, and R offers the user the opportunity to eith

R has several built-in datasets. To access them, we use the `data()` function after loading in the datasets library.

`library(datasets)`
```{r}
library(datasets)
`data()`
data()
```

To find out more about a specific dataset, we use the `?` operator.

Expand All @@ -54,14 +56,22 @@ Example:

#### Loading datasets

`data::dataset`
`datasets::dataset`

OR

`data(“dataset)`
`datasets("dataset")`

Example:

```{r}
datasets::airquality
```

```{r}
data("airquality")
```

#### Print datasets

`print(dataset)`
Expand Down
Loading

0 comments on commit 1d481d7

Please sign in to comment.