Your First R Program

📘 Introduction

After successfully installing R and RStudio, the next step is to write and execute your first R program. Every programming language traditionally begins with a simple Hello, World! program, which introduces the basic syntax and demonstrates how to display output on the screen. In R, this is accomplished using the print() function.

Information

The print() function displays values or messages in the Console, allowing you to verify that your program is working correctly.

🚀 Creating Your First R Script

👋 Hello, World! Program

The following program prints a simple message to the console.

Hello World Program

print("Hello, World!")

Output

Console Output

[1] "Hello, World!"

Success

If the message appears in the Console, your R installation is working correctly.

🔍 Understanding the Program

CodeDescription
print()Displays output in the Console.
"Hello, World!"A character string enclosed in double quotation marks.

đŸ–Ĩ Running Code in Different Ways

Type commands directly into the Console and press Enter. The code executes immediately.

Running in Console

print("Learning R")

Write code in an .R file and execute one line or multiple selected lines using Ctrl + Enter.

Script Example

print("Welcome")
print("This is my first script.")

Execute the complete script using the Source button in RStudio.

Run Entire Script

print("Program Started")
print("Processing...")
print("Program Finished")

📝 Writing Comments

Comments help explain code and improve readability. Anything written after the # symbol is ignored by the R interpreter.

Comments in R

# This is a single-line comment

print("Hello, World!")

# Display another message
print("Welcome to R Programming")

Tip

Use comments to explain why the code exists rather than simply describing what it does.

đŸ“Ļ Printing Different Types of Values

Printing Values

print("R Programming")
print(100)
print(45.67)
print(TRUE)

Output

Console Output

[1] "R Programming"
[1] 100
[1] 45.67
[1] TRUE

🔤 Using Variables

Instead of printing fixed values, you can store data in variables and print them whenever needed. Variables make programs more flexible and reusable.

Variables Example

name <- "Alice"
age <- 22

print(name)
print(age)

Output

Console Output

[1] "Alice"
[1] 22

➕ Simple Arithmetic Program

R can perform mathematical calculations directly.

Arithmetic Example

a <- 15
b <- 10

print(a + b)
print(a - b)
print(a * b)
print(a / b)

Output

Console Output

[1] 25
[1] 5
[1] 150
[1] 1.5

🧭 Program Execution Flow

âœī¸ Write the R code
Save the file
Execute the code
R Interpreter reads the program
Statements execute one by one
Output appears in the Console

âš ī¸ Common Beginner Mistakes

MistakeSolution
Missing quotation marks around textAlways enclose strings in double or single quotes.
Typing Print() instead of print()R is case-sensitive; function names must match exactly.
Forgetting parenthesesUse print() with opening and closing parentheses.
Not saving the scriptSave your work regularly with the .R extension.

💡 Best Practices

  • Use meaningful variable names.
  • Write short and readable programs.
  • Include comments where appropriate.
  • Save scripts frequently.
  • Test your code after making changes.

Best Practice

Develop the habit of writing, running, and testing small pieces of code frequently instead of writing large programs all at once.

🌍 Real-World Example

The following program displays a student's basic information by storing values in variables and printing them to the console.

Student Information

student_name <- "John"
course <- "Data Science"
semester <- 2

print(student_name)
print(course)
print(semester)

📝 Summary

Your first R program introduces the essential concepts of writing and executing code using the print() function. You learned how to create an R script, display text and values, use variables, perform basic arithmetic, write comments, and execute programs in RStudio. These foundational skills prepare you for more advanced topics such as data types, operators, control structures, functions, and data analysis.

>>"Every expert programmer started with a single line of code. Keep practicing, and each program will build your confidence."