Next →

Output:


      

Plots:

Your First R Script

In R, you can write code directly in the console for quick testing or save it into a script file (.R) to run multiple commands at once. Scripts help keep your code organized and reusable.

R scripts can include comments (lines starting with #) to explain what your code is doing. Comments are ignored when the script runs, but they make it easier for you (and others) to understand your work later.

Here’s an example of a simple R script that stores data in a variable and performs a calculation:

# This is my first R script
# Store a number in a variable
my_number <- 42

# Multiply the number by 2
result <- my_number * 2

# Print the result
print(result)

When you run this script, the output will be:

[1] 84

Another Example: Greeting the User

We can also store text in a variable and use it to print a personalized message:

# Store your name in a variable
name <- "Alex"

# Print a greeting message
print(paste("Hello,", name, "!"))

This code will print:

[1] "Hello, Alex !"