R Programming: Basic Syntax and Operations
Welcome to the foundational elements of R programming! R is a powerful language widely used for statistical computing and graphics. This module will introduce you to the essential syntax and operations that form the bedrock of your R journey.
Understanding R's Environment
When you open R or an R environment like RStudio, you'll interact with a console. This is where you type commands and see the output. R executes commands line by line, making it interactive and great for experimentation.
The R console.
Basic Arithmetic Operations
R handles standard arithmetic operations with familiar symbols. You can perform addition (
+
-
*
/
^
**
%%
R supports standard mathematical operators for calculations. For example, 2 + 3
will result in 5
, and 10 / 2
will yield 5
. Exponentiation can be done with 2^3
(which is 2 cubed, or 8). The modulo operator %%
gives the remainder of a division, so 10 %% 3
would be 1
.
Text-based content
Library pages focus on text content
The ^
or **
operator.
Variables and Assignment
Variables are used to store data. In R, you assign values to variables using the assignment operator, which is typically
<-
=
<-
For example,
x <- 10
x
x
The <-
assignment operator is the preferred way to assign values to variables in R, promoting clarity and consistency.
The <-
operator.
Comments in R
Comments are essential for making your code understandable. In R, anything following a hash symbol (
#
By using the hash symbol (#
).
Basic Data Types
R has several fundamental data types, including:
- Numeric: Represents numbers (e.g., 10, 3.14).
- Integer: Represents whole numbers (e.g., 5L). The suffix denotes an integer.codeL
- Character (or String): Represents text (e.g., "hello").
- Logical: Represents Boolean values (orcodeTRUE).codeFALSE
R Data Type | Description | Example |
---|---|---|
Numeric | Real numbers (integers and decimals) | 15, 2.718 |
Integer | Whole numbers | 10L |
Character | Text strings | "R Programming" |
Logical | Boolean values | TRUE, FALSE |
Character (or String).
Introduction to Vectors
Vectors are the most fundamental data structure in R. They are sequences of elements of the same basic type. You can create vectors using the
c()
For instance,
my_vector <- c(1, 2, 3, 4, 5)
The c()
function.
Basic String Operations
Strings (character data) can be concatenated using the
paste()
Example:
greeting <- "Hello"
name <- "World"
paste(greeting, name)
"Hello World"
The paste()
function.
Learning Resources
This chapter from 'R for Data Science' provides an excellent overview of R's philosophy and basic operations, setting the stage for data analysis.
The official R manual's introductory chapter covers basic syntax, data types, and operations in detail, offering a comprehensive reference.
A free introductory course that walks through R's basic syntax, data types, and fundamental operations with interactive exercises.
A clear video tutorial explaining R's basic syntax, including variables, operators, and data types.
This comprehensive blog post covers R programming fundamentals, including syntax, data types, and basic operations, suitable for beginners.
Explains R's core data types and how to use variables for storing and manipulating data.
A blog post specifically detailing how to create and work with vectors in R, a crucial data structure.
A straightforward guide to understanding the various operators available in R for arithmetic, logical, and assignment operations.
While focused on RStudio, this cheatsheet covers essential R commands and syntax that are fundamental to using the IDE effectively.
An audited option for a university-level introduction to R, covering basic syntax, data structures, and operations.