2+4#> [1] 6
When you type something into the console, R will give you a reply. For example:
2+4#> [1] 6
As expected, R returned 6.
[1] you see in the output. It simply means that 6 is the “first element of the line.”R can also be used as a calculator.
(59 + 73 + 2) / 3#> [1] 44.66667
10^(3+1)#> [1] 10000
| Description | Operator |
|---|---|
| Addition | + |
| Subtraction | - |
| Multiplication | * |
| Division | / |
| Exponentiation |
^ or **
|
| Integer division |
x %/% y (e.g., 10%/%3 is 3) |
| Modulus (remainder) |
x %% y (e.g., 10%%3 is 1) |
These calculations do not create objects that are remembered by R.
To store calculations in R, we assign them to objects.
Assignment operators <- or = can be used, but <- is preferred because = has other uses.
my_obj <- 48Now that we’ve created this object, R will remember it during the current session.
All created objects are stored in the current workspace. In RStudio, you can see them under the Environment tab (top right pane).


Objects can hold different types of values. For example:
my_obj2 <- "R is cool"Here we created my_obj2, which stores a character string "R is cool".
Strings must be in quotes.
If you forget, R will return an error.
my_obj2 <- R is cool#> Error in parse(text = input): <text>:1:14: unexpected symbol
#> 1: my_obj2 <- R is
#> ^
my_obj and my_obj2.
To change the value of an existing object, reassign it:
my_obj2 <- 1024Now my_obj2 holds a numeric value instead of a string.
You can perform operations using objects. For example:
my_obj3 <- my_obj + my_obj2
my_obj3#> [1] 1072
This creates my_obj3 with value
If you try adding two character strings:
char_obj <- "hello"
char_obj2 <- "world!"
char_obj3 <- char_obj + char_obj2#> Error in char_obj + char_obj2: non-numeric argument to binary operator
R will return an error because strings cannot be added.
Another common error is object * not found.
my_obj <- 48
my_obj4 <- my_obj + no_obj#> Error: object 'no_obj' not found
Here, no_obj has not been created, so the operation fails.
a-z, A-Z)0-9). and underscore _
-, or underscore _.mean, log, exp, TRUE, c, etc.).x1 with value 73.x2 as the result of x1 and x2 and store it in x3.x3 and calculate its 4th root.Parentheses ( ) are used to group expressions. They must be matched.
((3 + 12)/3 + 8)#> [1] 13
Curly braces { } do not cause errors in expressions but have special uses (e.g., functions).
{10 + 2} + 5#> [1] 17
Square brackets [ ] are used for indexing, not arithmetic:
[2 + 7]/3#> Error in parse(text = input): <text>:1:1: unexpected '['
#> 1: [
#> ^
| Description | Function | Example |
|---|---|---|
| Square root | sqrt |
sqrt(225) |
| Natural log | log |
log(50) |
| Exponential | exp |
exp(3) |
| Absolute value | abs |
abs(-10) |
| Factorial | factorial |
factorial(6) |
| Sine function | sin |
sin(25) |
| Inverse cosine | acos |
acos(-1) |
General function structure:
function_name(arg1 = val1, arg2 = val2, ...)Example:
Both return 5.
( ).round(3.567, digits=2)#> [1] 3.57
floor(3.567)#> [1] 3
ceiling(3.567)#> [1] 4
pi # Not a function, but useful#> [1] 3.141593
(1+2) ^ 3#> [1] 27
( 1+ 2) ^ 3#> [1] 27
You can add spaces freely between elements.
But spaces inside a value cause errors:
3 .14#> Error in parse(text = input): <text>:1:5: unexpected numeric constant
#> 1: 3 .14
#> ^
Use # to write comments. R ignores everything after #.
# Calculating BMI
weight <- 70 # in kg
height <- 1.7 # in meters
bmi <- weight / height ^ 2
bmi#> [1] 24.22145
Instead of typing code line by line, you can save it in a script (.R file).
source("my_script.R")source("C:/Users/Rasel/Documents/my_script.R")
Rscript my_script.Rsource() runs inside your current R session (objects remain).Rscript runs in a fresh session (objects do not remain).Why does this code not work?
my_variable <- 10
my_varıable#> Error: object 'my_varıable' not found
myObject with a value between 1 and 100.myObject and update the object.myObject is divisible by 2, 3, 13, or 21.myObject?Save this code into practice.R:
x <- 5
y <- 10
print(x + y)Run it with source("practice.R") (R Console).
Run it with Rscript practice.R (Terminal).