Skip to content

Commit

Permalink
Fix typos in Episode 1
Browse files Browse the repository at this point in the history
  • Loading branch information
melissavanbussel committed Oct 22, 2024
1 parent 25fed67 commit 007d1b4
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions episodes/01-programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exercises: 0

- What is programming?
- What is object oriented programming?
- How to document code?
- How do I document code?
- What is a directory?

::::::::::::::::::::::::::::::::::::::::::::::::::
Expand All @@ -33,7 +33,7 @@ There are a few reasons why programming with R is useful for official statistics

## R is an object oriented programming language

Object oriented programming languages use *objects* as their main tools. These *objects* have classes, which describe their general properties. For example, in R you might work with *numeric* objects, which would contain numbers. You could also work with *characters*, which would be composed of text. We'll explore classes and data types thoroughly in Episode 3 (Data Types and Structures). We can assign "labels" to these objects, creating a *variable* and use them interchangeably. We assign objects with an assignment operator. In R, the most commonly used assignment operator is `<-`. Try reproducing the example below on your machine by entering the code into the console and hitting the "run" button.
Object oriented programming languages use *objects* as their main tools. These *objects* have classes, which describe their general properties. For example, in R you might work with *numeric* objects, which would contain numbers. You could also work with *characters*, which would be composed of text. We'll explore classes and data types thoroughly in Episode 3 (Data Types and Structures). We can assign "labels" to these objects, creating a *variable* and use them interchangeably. We assign objects with an assignment operator. In R, the most commonly used assignment operator is `<-`. Try reproducing the example below on your machine by entering the code into the RStudio "Console" and hitting "Enter".

```r
# Assign a number to a variable
Expand All @@ -45,20 +45,21 @@ print(number_flowers)

We can get the value stored within the variable by printing it.

```source
```output
[1] 8
```

Assigning a new value to a variable breaks the connection with the old value; R forgets that number and applies the variable name to the new value.

When you assign a value to a variable, R only stores the value, not the calculation you used to create it. This is an important point if you're used to the way a spreadsheet program automatically updates linked cells. Let's look at an example.

```
```r
# Reassign the variable
number_flowers <- 7
```

{: .language-r)
# Print the variable's contents
print(number_flowers)
```

```output
[1] 7
Expand All @@ -68,21 +69,20 @@ number_flowers <- 7

## Variable Naming Conventions

Historically, R programmers have used a variety of conventions for naming variables. The `.` > character in R can be a valid part of a variable name; thus the above assignment could have
Historically, R programmers have used a variety of conventions for naming variables. The `.` character in R can be a valid part of a variable name; thus the above assignment could have
easily been `weight.kg <- 57.5`. This is often confusing to R newcomers who have programmed
in languages where `.` has a more significant meaning.
Today, most R programmers 1) start variable names with lower case letters, 2) separate words > in variable names with underscores, and 3) use only lowercase letters, underscores, and
numbers in variable names. The *Tidyverse Style Guide* includes
Today, most R programmers 1) start variable names with lower case letters, 2) separate words in variable names with underscores, and 3) use only lowercase letters, underscores, and
numbers in variable names. This is known as "snake case". The *Tidyverse Style Guide* includes
a [section](https://style.tidyverse.org/syntax.html) on this and other style considerations.


::::::::::::::::::::::::::::::::::::::::::::::::::

## Documenting Code

Notice that in the above examples, hashtags (`#`) are used before giving instructions that are intended for you rather than R. Hashtags produce *comments*, which are handy for leaving information about the code that will follow. Commenting as much code as possible is part of best practices. Always comment your code! You owe it to your colleagues who may see your code (not to mention your future coding self).

```-r
```r
# Hashtags go before commented code, which is not run

# print("This code will not be run")
Expand All @@ -100,26 +100,24 @@ A directory is a location on your machine. Say you'd like to open a file that's
For example: `/Users/Documents/Learning-R` points to a folder called "Learning-R" in a user's documents folder. Depending on your IDE (Integrated Development Environment) and setup, you can print your current directory, known as the *working directory*. R automatically reads and writes files from and to your current working directory.

```r
# print current working directory
# Print current working directory
getwd()
```

```output
[1] "/Users/Documents/
```

Before beginning our lessons, please set your working directory to the folder that we created in the setup section with `setwd()`. For example, if your folder is named Learning-R:
Before beginning our lessons, please set your working directory to the folder that we created in the setup section with `setwd()`. For example, if your folder is named `Learning-R`:

```r
# change current working directory
# Change current working directory
setwd("~/Documents/Learning-R")
```



:::::::::::::::::::::::::::::::::::::::: keypoints

- Programming makes our work faster, more reproducible, and more transparent.
- Programming makes our work faster, more reproducible, and more transparent
- R is an object oriented programming language
- Document your code with comments
- A working directory is the active location on your computer where R can read and write files
Expand Down

0 comments on commit 007d1b4

Please sign in to comment.