Your First R Program

Introduction

In this chapter, you will write and run your first R program. This simple program will help you get familiar with the basics of R programming and using RStudio as your development environment. We will cover writing a basic script, running the script, and performing simple operations.

Writing a Basic R Script

1. Open RStudio

  • Launch RStudio from your Applications folder (macOS), Start menu (Windows), or using the terminal (Linux).

2. Create a New R Script

  • Go to File > New File > R Script.
  • A new script editor window will open where you can write your R code.

3. Writing Your First R Program

In the script editor, type the following code:

# My First R Program

# Print a greeting message
print("Hello, R World!")

# Perform a simple calculation
result <- 3 + 5

# Print the result of the calculation
print(result)

# Create a sequence of numbers
numbers <- 1:10

# Print the sequence of numbers
print(numbers)

# Plot the sequence of numbers
plot(numbers, main="Simple Plot of Numbers")

Explanation of the Code

  • Comments: Lines starting with # are comments. They are not executed by R but are useful for adding notes to your code.
  • print: The print function is used to display messages and values.
  • Assignment: The <- operator is used to assign values to variables.
  • Sequence: 1:10 creates a sequence of numbers from 1 to 10.
  • Plot: The plot function is used to create a basic plot.

Running Your R Script

1. Save Your Script

  • Save your script by going to File > Save or pressing Ctrl+S (Windows/Linux) or Cmd+S (macOS).
  • Choose a location and give your script a name, such as first_program.R.

2. Run Your Script

  • To run the entire script, click the Source button at the top right of the script editor or press Ctrl+Shift+S (Windows/Linux) or Cmd+Shift+S (macOS).
  • You can also run individual lines or sections of the script by selecting the code and pressing Ctrl+Enter (Windows/Linux) or Cmd+Enter (macOS).

3. View the Output

  • The output of your script will be displayed in the Console pane at the bottom of RStudio.
  • You should see the following output:
[1] "Hello, R World!"
[1] 8
[1] 1 2 3 4 5 6 7 8 9 10
  • Additionally, a plot window will open displaying a simple plot of numbers from 1 to 10.

Performing Simple Operations

Basic Arithmetic

You can perform basic arithmetic operations directly in the console or in your script:

# Addition
sum <- 2 + 3
print(sum)  # Output: 5

# Subtraction
difference <- 5 - 2
print(difference)  # Output: 3

# Multiplication
product <- 4 * 3
print(product)  # Output: 12

# Division
quotient <- 10 / 2
print(quotient)  # Output: 5

Creating and Manipulating Vectors

Vectors are a basic data structure in R. You can create and manipulate vectors as follows:

# Create a vector
vec <- c(1, 2, 3, 4, 5)
print(vec)  # Output: 1 2 3 4 5

# Access elements of a vector
first_element <- vec[1]
print(first_element)  # Output: 1

# Perform operations on vectors
squared_vec <- vec^2
print(squared_vec)  # Output: 1 4 9 16 25

Using Functions

Functions in R perform specific tasks. You can use built-in functions or create your own:

# Use a built-in function
mean_value <- mean(vec)
print(mean_value)  # Output: 3

# Create a custom function
square <- function(x) {
  return(x^2)
}

# Use the custom function
result <- square(4)
print(result)  # Output: 16

Conclusion

Congratulations! You have written and run your first R program. You learned how to create a script, perform basic operations, manipulate vectors, and use functions. This foundational knowledge will help you as you continue to explore and learn more about R programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top