Key Concepts

Review core concepts you need to learn to master this subject

Mathematical Operations in R

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

In R, the conventional symbols +, -, *, and / are used for addition, subtraction, multiplication, and division. The PEMDAS order of operations governs these mathematical operations in R.

R Comments

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

R interprets anything on a line after a # symbol as a comment.

Comments can be used to add text in the program that will NOT be executed. They are useful for providing context, hints for yourself or others working on the code, or even to temporarily get rid of a line when debugging.

R Data Types

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

In R, and in programming in general, data types are the classifications that we give to different kinds of information pieces.

Specifically, R provides the following basic data types: character, numeric, integer, logical, and complex. Each data type is used to represent some type of info - numbers, strings, boolean values, etc.

R Numeric Data Type

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

The numeric data type in R is a class that represents numbers that can be integers or decimals.

R’s Character Data Type

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

R’s character data type includes all string values. A character is denoted by its surrounding quotation marks. Characters are any text or grouping of keyboard characters, including letters, numbers, spaces, symbols, etc. The character version of a number is the text conveying a number. The number itself, without quotes, is numeric.

R Logical Data Type

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

The R logical data type has two possible values - TRUE or FALSE.

It is important for the capitalization to stay as shown and to make sure not to wrap the values in quotes.

NA Data Type in R

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

R conveys an absence of value with the keyword NA (no quotes). There is a numeric NA, a character NA, and a logical NA. Each means a non value within its context.

Assignment Operator in R

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

In R, there are two ways to assign values to variables. We can use the assignment operator, an arrow sign (<-) made with a carat and a dash. It is also acceptable, though less preferred, to use an equal sign (=).

R Conditional Statements

# Results in "500" 573 - 74 + 1 # Results in "50" 25 * 2 # Results in "2" 10 / 5

In R, a conditional statement goes inside parentheses and is followed by a set of curly braces that contain the code to be executed.

In an if statement, if the conditional is True then the code inside the curly braces is executed.

Introduction to R Syntax
Lesson 1 of 1
  1. 1
    Ahoy! We R excited for you to start your learning adventure with a language built for data enthusiasts! The R community is made up of people passionate about the intersection of numbers, data, anal…
  2. 2
    Let’s start with the basic syntax for mathematical calculations in R. R performs addition, subtraction, multiplication, and division with +, -, *, and /: # Results in “500” 573 - 74 + 1 # Results…
  3. 3
    Ironically, the second thing we’re going to do is show you how to tell R to ignore a part of your program. We promise it’s very useful to know how to do this. Text written in a program but not run …
  4. 4
    Now that you know how to calculate basic math and add comments explaining your code, let’s dive into how R “thinks about” different types of data. In R and in programming, data types are the clas…
  5. 5
    Now that you know how R classifies some of the basic information types, let’s figure out how to store them. In programming, variables allow us to store information and associate that information wi…
  6. 6
    We mentioned Vectors when we introduced data types earlier. In R, vectors are a list-like structure that contain items of the same data type. Take a look here: spring_months <- c(“March”, “April”…
  7. 7
    In R, we will often perform a task based on a condition. For example, if we are analyzing data for the summer, then we will only want to look at data that falls in June, July, and September. We ca…
  8. 8
    When writing conditional statements, sometimes we need to use different types of operators to compare values. These operators are called comparison operators. Here is a list of some handy comparis…
  9. 9
    Working with conditionals means that we will be using logical, true or false values. In R, there are operators that work with logical values known as logical operators. We can use logical operators…
  10. 10
    Functions are actions we can perform. R provides a number of functions, and you’ve actually been using a few of them even though you maybe didn’t realize! We call, or use, these functions by sta…
  11. 11
    R’s popularity is also largely due to the many fantastic packages available in the language! A package is a bundle of code that makes coding certain tasks easier. There are all sorts of packages fo…
  12. 12
    Congrats on finishing your first R lesson! Here’s a summary of some of the concepts you’ve learned: + R is a powerful statistical programming language with a large community of data enthusiasts. …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo