Contents
How R Works
R Execution Lifecycle
Every R command follows a basic execution process.
<text weight="medium">The four main steps are:</text>
- Write the code: Enter an instruction in the script editor or Console.
- Evaluate the instruction: R checks the command's syntax and evaluates it.
- Execute the command: R performs the requested operation.
- View the result: The Console displays the output or an error message.
For example:
x <- 10
y <- 20
x + y
Output:
[1] 30
Errors are a normal part of programming. Read the error message carefully, as it often helps you identify what needs to be fixed.
Working with R Scripts
An R script is a text file with the .R extension that contains one or more commands.
Scripts are useful because you can save your code, make changes, and execute it again without retyping every instruction.
To create and run an R script in RStudio:
- Select File → New File → R Script.
- Type your R code in the editor.
- Save the file with a name such as first_program.R.
- Press Ctrl + Enter to run the current line or selected code.
- Click Source to run the entire script.
You can also execute a saved script from the Console:
source("first_program.R")
Comments help explain your code. In R, a comment begins with the # symbol and is ignored during execution.
For example:
# Store and display a student's marks
marks <- 85
print(marks)
Understanding Data Flow in R
Most data analysis projects follow a sequence of steps, from importing raw data to presenting useful findings.
The typical data analysis workflow is:
<text weight="medium">Import → Clean → Transform → Analyze → Visualize → Report</text>
- Import: Load data from a CSV file or another supported source.
- Clean: Identify and handle missing values, duplicates, and inconsistent data.
- Transform: Select columns, filter rows, and create calculated variables.
- Analyze: Calculate statistics and investigate patterns.
- Visualize: Create charts to communicate important findings.
- Report: Present the results clearly.
Each step builds on the previous one, helping you turn raw data into meaningful insights.
Example: In the Student Performance Analysis project, you will import student records, calculate average marks, examine attendance, and create visualizations to understand the results.
R for Data Science
Harini A








