[![](https://img.shields.io/badge/download%20html-blue)](../rendered-html/r_basics.html)
[![](https://img.shields.io/badge/download%20Rmd-blue)](r_basics.Rmd)

```{r setup, include=FALSE}
# This chunk sets up global options for all code chunks in the document
knitr::opts_chunk$set(echo = TRUE)  # Makes code visible in output
```

## Getting Started with R

R is a powerful programming language designed for statistical computing and data analysis. This tutorial will cover the fundamental concepts you need to get started with R programming.

### Using R as a Calculator

```{r calculator}
# Basic arithmetic operations demonstration
2 + 2       # Addition operator (+)
10 - 5      # Subtraction operator (-)
4 * 3       # Multiplication operator (*)
15 / 3      # Division operator (/)
2^3         # Exponentiation operator (^) - 2 raised to power 3

# Print results with descriptive labels using print() function
print("Addition: 2 + 2 =")          # Print a text label
print(2 + 2)                        # Print the result
print("Exponentiation: 2^3 =")      # Print another label
print(2^3)                          # Print the result
```

### Objects and Variables

```{r objects}
# Creating objects using different assignment operators
x <- 10                # Using <- (preferred in R)
my_number = 42         # Using = (works but <- is more common in R)
text <- "Hello, R!"    # Assigning a string value

# Demonstrating how to print object values
print("Value of x:")                 # Print label
print(x)                            # Print numeric value
print("Value of my_number:")         # Print label
print(my_number)                    # Print numeric value
print("Value of text:")              # Print label
print(text)                         # Print string value
```

### Data Types in R

```{r data-types}
# Demonstrating different basic data types in R
age <- 25                           # Numeric type (includes integers and decimals)
name <- "John"                      # Character type (for text/strings)
is_student <- TRUE                  # Logical type (TRUE/FALSE)

# Using class() function to check data types
print("Type of 'age':")
print(class(age))                   # Shows "numeric"
print("Type of 'name':")
print(class(name))                  # Shows "character"
print("Type of 'is_student':")
print(class(is_student))            # Shows "logical"
```

### Vectors

```{r vectors}
# Creating vectors using the combine function c()
numbers <- c(1, 2, 3, 4, 5)         # Numeric vector
fruits <- c("apple", "banana", "orange")  # Character vector

# Demonstrating vector operations (vectorization)
numbers_plus_2 <- numbers + 2       # Adds 2 to each element
numbers_times_3 <- numbers * 3      # Multiplies each element by 3

# Demonstrating vector indexing (R uses 1-based indexing)
first_number <- numbers[1]          # Get first element using []
second_fruit <- fruits[2]           # Get second element using []

# Print results to show vector operations
print("Original numbers vector:")
print(numbers)
print("Numbers + 2:")               # Show vectorized addition
print(numbers_plus_2)
print("Numbers * 3:")               # Show vectorized multiplication
print(numbers_times_3)
print("First number:")              # Show indexing result
print(first_number)
print("Second fruit:")              # Show indexing result
print(second_fruit)
```

### Saving Objects

```{r saving-objects}
# Create example objects to demonstrate saving
numbers <- c(1, 2, 3, 4, 5)
fruits <- c("apple", "banana", "orange")

# Demonstrate different ways to save R objects
save(numbers, file = "my_numbers.RData")     # Save single object
save(numbers, fruits, file = "my_objects.RData")  # Save multiple objects
save.image(file = "workspace.RData")         # Save entire workspace

# Demonstrate loading saved objects
load("my_numbers.RData")                     # Load saved data
print("Loaded numbers:")
print(numbers)
```

### Working with Files

```{r files}
# Create a sample data frame for file operations
df <- data.frame(
  numbers = 1:5,                    # Sequence 1 to 5
  letters = LETTERS[1:5]            # First 5 uppercase letters
)

# Demonstrate writing data to CSV
write.csv(df, file = "example.csv", row.names = FALSE)

# Demonstrate reading data from CSV
data <- read.csv("example.csv")     # Read the file we just created
print("Data read from CSV:")
print(data)                         # Show the loaded data
```

### Getting Help

```{r help, eval=FALSE}
# Demonstrate different ways to get help in R
?mean           # Access help page for mean function
help(sum)       # Alternative way to access help
example(mean)   # See examples of using mean function
```

## Practice Exercises

### Exercise 1: Create and Manipulate a Vector
```{r exercise1}
# Create a sequence vector using : operator
my_vector <- 1:10                   # Creates vector from 1 to 10

# Calculate the arithmetic mean
vector_mean <- mean(my_vector)      # Use mean() function

# Display results
print("My vector:")
print(my_vector)
print("Mean of my vector:")
print(vector_mean)
```

### Exercise 2: Working with Different Data Types
```{r exercise2}
# Create a character vector
colors <- c("red", "blue", "green")  # Vector of color names

# Create a list to hold multiple data types
my_list <- list(
  numbers = my_vector,              # Add numeric vector
  colors = colors                   # Add character vector
)

# Show the structure of the list
print("My list:")
print(my_list)
```

## Next Steps

After mastering these basics, you can move on to:
- Data frames and tibbles
- Basic plotting with base R
- Installing and using packages
- Basic statistical functions
- Control structures (if/else, loops)

```{r cleanup, include=FALSE}
# Clean up temporary files created during the tutorial
file.remove("my_numbers.RData")      # Remove saved number data
file.remove("my_objects.RData")      # Remove saved objects
file.remove("workspace.RData")       # Remove workspace
file.remove("example.csv")           # Remove CSV file
``` 