Week 1: Your First Steps with R
Discover the R language and set up your RStudio environment.
Start LearningSetting up Your Environment: Installing R & RStudio.
To start coding in R, you need two key components: R itself (the language interpreter) and RStudio (an Integrated Development Environment - IDE - that makes working with R much easier).
Step 1: Installing R
R must be installed first. Download the appropriate version for your operating system from the official Comprehensive R Archive Network (CRAN).
- Go to https://cran.r-project.org/
- Click on the link corresponding to your operating system (Linux, macOS, or Windows).
- Follow the instructions on the page to download and install the latest base distribution of R. Accept the default settings during installation.
Step 2: Installing RStudio
RStudio provides a user-friendly interface with features like a code editor, console, environment viewer, plotting window, package manager, and debugger.
- Go to the Posit website (the company behind RStudio): https://posit.co/download/rstudio-desktop/
- Download the free RStudio Desktop version suitable for your operating system.
- Run the installer, accepting the default settings. RStudio should automatically detect your R installation.
Once both are installed, launch RStudio. This will be your primary workspace for learning and using R.
Basic Syntax and Conventions: The Rules of R.
Like any language, R has its grammar and style. Understanding these basics is key to writing clear and functional R code.
- Comments: Explain your code using comments. In R, comments start with the hash symbol `#`. Everything after `#` on that line is ignored by R.
# This is a comment explaining the code below radius <- 5 # Assigning the value 5 to the variable 'radius' - Case Sensitivity: R is case-sensitive. `myVariable`, `MyVariable`, and `myvariable` are treated as distinct variables.
- Statements: R code is executed statement by statement, typically one per line. You can put multiple statements on one line separated by semicolons `;`, but this is generally less readable.
a <- 1; b <- 2; print(a+b) # Less common style - Assignment Operator: The preferred assignment operator in R is `<-`. While `=` also works for assignment in most contexts, `<-` is stylistically preferred and less ambiguous (as `=` is also used for function arguments).
x <- 100 # Preferred style y = 200 # Also works, but less conventional - Blocks and Braces `{}`: Unlike Python's reliance on indentation[cite: 10], R uses curly braces `{}` to group multiple statements into a block, often used with control flow structures (like `if`, `for`) and functions. Indentation is still crucial for readability but doesn't define the block structure syntactically.
if (x > 50) { print("x is large") print("This is part of the if block") } - Variable Naming Conventions:
- Should be descriptive.
- Can contain letters, numbers, dots (`.`), and underscores (`_`).
- Must start with a letter or a dot (if starting with a dot, the second character cannot be a number).
- Common styles include `snake_case` (e.g., `user_age`) or using dots (e.g., `user.age`). Consistency is key.
- Avoid using names of built-in functions (e.g., `c`, `list`, `mean`).
Getting comfortable with these basic rules will make your R learning journey smoother.