0% found this document useful (0 votes)
5 views10 pages

R Programming First Class Teaching Notes

The document outlines a 60-minute lesson plan for teaching beginners the basics of R programming and RStudio. Key topics include basic commands, variables, vectors, and saving files, with a focus on hands-on practice and interaction. Learning outcomes emphasize understanding R's applications, using RStudio's interface, and executing simple commands and functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

R Programming First Class Teaching Notes

The document outlines a 60-minute lesson plan for teaching beginners the basics of R programming and RStudio. Key topics include basic commands, variables, vectors, and saving files, with a focus on hands-on practice and interaction. Learning outcomes emphasize understanding R's applications, using RStudio's interface, and executing simple commands and functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R PROGRAMMING

First Class Teaching Notes

A simple 60-minute lesson plan for beginners

Course Introduction to R Programming

Class First classroom session

Duration Approximately 60 minutes

Main focus RStudio, basic commands, variables, vectors, scan(), and


saving files

Prepared for classroom teaching and live demonstration


1. What to Teach in the First Class
The first class should make students comfortable with R and RStudio. Avoid beginning with advanced
statistics or complex programs. Students should first learn where to type a command, how to run it, how
R stores a value, how to create a vector, and how to save their work.

Learning outcomes
 Explain what R is and mention a few areas in which it is used.
 Identify the Source, Console, Environment, Files, and Plots panes in RStudio.
 Run a command using Ctrl + Enter and understand the Console prompt.
 Create variables and perform simple arithmetic operations.
 Create a vector and apply basic functions such as sum(), mean(), min(), max(), and length().
 Read numbers from the keyboard using scan().
 Save an R script and save simple data to a CSV file.

Suggested 60-minute class plan


Time Topic Teaching activity
0-5 min Introduction Introduce the course and ask students
where programming is used.
5-12 min R and RStudio Explain the difference between R and
RStudio.
12-20 min RStudio interface Show Source, Console, Environment,
Files, and Plots.
20-32 min First commands Demonstrate arithmetic, variables,
assignment, and output.
32-43 min Vectors and functions Create a marks vector and calculate
simple summaries.
43-50 min Keyboard input Demonstrate scan() carefully, one
command at a time.
50-56 min Saving work Save the script and export data as a
CSV file.
56-60 min Practice and recap Give two short exercises and review
key points.
2. Opening the Class
Suggested opening statement
Teacher can say: Good morning. Today we are beginning R programming. R is a programming
language used for data analysis, statistics, machine learning, and visualization. In this first class, the aim
is not to write a large program. The aim is to understand the RStudio screen, run simple commands, store
values, and save our work.

Simple questions to involve students


 Where do we see data in daily life?
 Which software tools have you used for calculations or data analysis?
 What is the difference between entering a calculation in a calculator and writing a reusable
program?

What is R?
R is a programming language and software environment mainly used for statistical computing, data
analysis, machine learning, and graphical presentation. It allows users to enter commands, work with
data, perform calculations, and create plots.

What is RStudio?
RStudio is an integrated development environment, commonly called an IDE. It provides a convenient
screen for writing R programs, running commands, viewing variables, managing files, and displaying
graphs. R is the language; RStudio is the working interface used to write and run R code.

Important distinction: Installing only R gives the R Console. Installing RStudio provides a more
convenient interface, but R must also be installed because RStudio uses the R language.

Applications of R
 Data analysis and statistical calculations
 Data visualization and report preparation
 Machine learning and predictive modelling
 Research, business analytics, finance, healthcare, and education
3. Understanding the RStudio Window
Open RStudio and point to each pane while explaining its purpose. Students learn faster when the
explanation is connected directly to the screen in front of them.
RStudio area Purpose
Source or Script pane Used to write and save several lines of R code. A
script normally has the .R extension.

Console Used to execute commands and display results. The >


symbol is the R prompt.

Environment Displays variables and data objects created during the


current session.

History Shows commands that were executed previously.

Files Displays files and folders in the current working


location.

Plots Displays graphs generated by R.

Packages and Help Used to manage additional R packages and read


function documentation.

Essential keyboard buttons


Keyboard shortcut Use
Ctrl + Enter Run the current line or selected code.
Ctrl + S Save the current R script.
Ctrl + L Clear the Console display.
Up Arrow Recall the previously executed command.
Esc Stop the current command or cancel scan() input.

Classroom instruction: Ask students to type code in the Source pane and use Ctrl + Enter. Use the
Console mainly for entering input requested by functions such as scan().
4. Running the First R Commands
Step 1: Use R as a calculator

10 + 20
50 - 15
6*8
100 / 4
2^5

Explain that +, -, *, /, and ^ represent addition, subtraction, multiplication, division, and exponentiation.

Step 2: Create variables

x <- 10
y <- 20
result <- x + y
result

The symbol <- is the assignment operator. It stores the value on the right side in the variable on the left
side. The equal sign can also work in many situations, but <- is the standard assignment symbol in R.

Important: R is case-sensitive. The variables marks, Marks, and MARKS are treated as three different
names.

Step 3: Display output

print(result)
cat("The total is", result, "\n")

print() displays an R object. cat() joins text and values to create a simple sentence. The symbol \n
moves the cursor to the next line.

Step 4: Introduce basic data types

student_name <- "Ravi" # character


age <- 19 # numeric
passed <- TRUE # logical

For the first class, introduce only three basic types: numeric values, character text placed inside
quotation marks, and logical values TRUE or FALSE.

Two-minute student practice: Create variables length = 8 and width = 5. Calculate the area of a
rectangle and display the result using print().
5. Vectors and Basic Functions
A vector stores multiple values of the same basic type. The c() function combines values into a vector.
This is one of the most important ideas in R and is suitable for the first class.

marks <- c(75, 82, 68, 90, 79)


marks

Apply simple functions

sum(marks)
mean(marks)
min(marks)
max(marks)
length(marks)

Function Meaning
sum(marks) Adds all values.

mean(marks) Calculates the average.

min(marks) Finds the smallest value.

max(marks) Finds the largest value.

length(marks) Counts the number of values.

Access individual values

marks[1] # first value


marks[3] # third value
marks[2:4] # values from position 2 to 4

R starts vector positions from 1, not from 0. The square brackets select values according to their
positions.

Student activity: Ask students to create a vector containing five subject marks and find the total,
average, highest mark, and lowest mark.

Optional demonstration: a simple graph

barplot(marks,
main = "Student Marks",
xlab = "Subject",
ylab = "Marks")

Use this only as a brief motivational demonstration. Detailed plotting can be taught in a later class.
6. Reading Values from the Keyboard Using scan()
The scan() function reads a sequence of values. By default, it expects numeric values. Because scan()
waits for keyboard input, the command must be run separately.

Correct procedure
1. Type x <- scan() in the Source pane.
2. Place the cursor on that line and press Ctrl + Enter.
3. Click inside the Console when the prompt 1: appears.
4. Type numeric values separated by spaces, for example 10 20 30 40 50.
5. Press Enter once after the values.
6. Press Enter again on an empty line to finish the input.
7. Run x to display the stored vector.

x <- scan()
# Enter in the Console: 10 20 30 40 50
# Press Enter, then press Enter again.
x
mean(x)

Reading character values

student_names <- scan(what = character())


# Enter: Ravi Sita Ramesh
# Press Enter, then press Enter again.
student_names

Common error and explanation

Error in scan(): scan() expected 'a real', got 'scan()'

This error occurs when several lines are run together. After the first scan() begins, R expects numeric
input. The following line of code is then mistakenly treated as input. Run each scan() command
separately, enter the values in the Console, and finish by pressing Enter twice.

Teacher reminder: Do not select x <- scan(), x, and y <- scan() together. Run x <- scan() alone,
complete its input, and only then run the next command.
7. Saving the R Program and Data
Save the R script
8. Click inside the Source pane.
9. Press Ctrl + S, or choose File > Save As.
10. Select a folder and enter a suitable filename, such as First_Class.R.
11. Click Save. The .R file stores the program commands.

Check the current working folder

getwd()

The result shows the folder in which R reads and writes files when only a filename is supplied.

Save values as a CSV file

student <- c("Asha", "Ravi", "Sita")


marks <- c(78, 85, 91)

data <- [Link](student, marks)


[Link](data, "student_marks.csv", [Link] = FALSE)

The CSV file can be opened using spreadsheet software. In RStudio, students can also use the Files
pane to locate the file.

Save R objects

save(marks, file = "[Link]")

# Load the object in a later session


load("[Link]")

First-class priority: It is enough to demonstrate saving an .R script and one CSV file. Saving .RData can
be mentioned briefly and practised later.

Recommended file names


 Use meaningful names such as First_Class.R or Student_Marks.csv.
 Avoid special symbols in filenames.
 Keep the script and related data files in one clearly named folder.
8. Complete Classroom Demonstration
Type the following program gradually. Explain each line before running it. Do not paste the complete
program at once during the first demonstration.

# First R program
student_name <- "Ravi"
marks <- c(75, 82, 68, 90, 79)

total <- sum(marks)


average <- mean(marks)
highest <- max(marks)

cat("Student:", student_name, "\n")


cat("Marks:", marks, "\n")
cat("Total:", total, "\n")
cat("Average:", average, "\n")
cat("Highest mark:", highest, "\n")

results <- [Link](


Student = student_name,
Total = total,
Average = average,
Highest = highest
)

[Link](results, "first_class_result.csv", [Link] = FALSE)

Questions to ask while demonstrating


 Which line creates the vector?
 What value is stored in total?
 Why is the student name placed inside quotation marks?
 What is the purpose of [Link]()?
 Where will the CSV file be saved?

Expected concepts covered


This single demonstration connects variables, vectors, functions, output, a data frame, and file saving. It
gives students a complete but manageable view of how a small R program works.
9. Practice, Homework, and Recap
In-class practice
12. Create two variables a = 25 and b = 15. Display their sum, difference, product, and quotient.
13. Create a vector containing five numbers. Find its sum, average, minimum, maximum, and length.
14. Use scan() to enter five marks from the keyboard. Store them in marks and calculate the average.
15. Save the script as R_First_Class.R.

Homework
Write an R program that stores the name of a student and five subject marks. Calculate the total,
average, highest mark, and lowest mark. Display all results clearly and save them in a CSV file named
Student_Result.csv.

Five-minute recap
 R is the programming language; RStudio is the working interface.
 Ctrl + Enter runs the current line or selected code.
 <- assigns a value to a variable.
 c() creates a vector.
 scan() reads values from the keyboard and must be run separately.
 Ctrl + S saves the script, and [Link]() saves data as a CSV file.

Teacher checklist before class


 Confirm that R and RStudio open correctly on the classroom computer.
 Create a new R script before students arrive.
 Keep a folder ready for saving the script and CSV file.
 Test Ctrl + Enter and scan() once before the class.
 Keep the first class practical and interactive; avoid too many definitions.
Final teaching advice: The main success of the first class is not the number of topics completed.
Students should leave the class confident that they can open RStudio, run a command, create a vector,
read keyboard input, and save their work.

You might also like