R Syntax
R syntax defines the rules for writing programs and scripts in R. The language is case-sensitive, and instructions are given through functions and operators. Let's look at the basic syntax and how to print output in R.
Key Topics
1. Basic Syntax
R code is written as a series of expressions. Here's a basic 'Hello World' example:
Example: Hello World in R
# This is a basic R script
print("Hello, World!")
Output:
[1] "Hello, World!"
Code Explanation: The print()
function is used to display output in R. Here, it prints the string 'Hello, World!' to the console.
2. Printing Output
You can use the print()
function or simply type an expression to view the result. Here's another example:
Example: Basic Arithmetic in R
# Performing arithmetic operations
result <- 5 + 3
print(result)
Output:
[1] 8
Code Explanation: The result of the arithmetic operation is stored in the variable result
and printed using print()
.
Key Takeaways
- R is case-sensitive, and functions like
print()
are used to display output. - Basic syntax in R is straightforward, and expressions are evaluated to produce results.