đ 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
đ 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
đ Understanding the Program
| Code | Description |
|---|---|
| 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
đĻ 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
â ī¸ Common Beginner Mistakes
| Mistake | Solution |
|---|---|
| Missing quotation marks around text | Always enclose strings in double or single quotes. |
| Typing Print() instead of print() | R is case-sensitive; function names must match exactly. |
| Forgetting parentheses | Use print() with opening and closing parentheses. |
| Not saving the script | Save 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
đ 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.