Introduction to R
August 15, 2023 · 2 min read
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!
Common Arithmetic Operators #
| Operator | Description |
|---|---|
| + | Addition |
| − | Subtraction |
| * | Multiplication |
| / | Division |
| ^ or ** | Exponentiation |
| %% | Remainder |
| %/% | Integer division |
Note that R is case-sensitive.
Common Mathematical Functions #
- abs(x)
- sqrt(x)
- sin(x), cos(x), tan(x)
- asin(x), acos(x), atan(x)
- exp(x)
- log(x), log2(x), log10(x)
- round(x, number of decimal places to retain)
- ceiling(x)
- floor(x)
- trunc(x): Extract the integer part of x
- Most functions in R packages come with examples, and the function example ( ) is used to run example code.
R Objects #
In the R language, “everything is an object.” Data analysis includes many steps, from data organization and exploration to modeling and visualization, and each step requires handling different objects, such as vectors, matrices, functions, models, and so on.
a = 3 + 5
# Can be written as
a <- 3 + 5
It is recommended to use the
<-assignment symbol to avoid confusion with the comparison operator==
b <- sqrt(36) #b=6
a + b
# You can also calculate its value on the left, and then assign the result to a new object through right assignment "->"; this notation is uncommon
a + 3*b -> c
c
An object’s name can consist of one or more characters. An object name generally can only start with a letter and can contain numbers, periods “.”, and underscores “_”.
Common Relational and Logical Operators #
>- <
- ==
- !=
>=- <=
- &
- |
- !
Workspace Management #
The workspace is R’s working environment, and all created objects are temporarily saved in the workspace (also called the global environment, .GlobalEnv).
We can use the function ls( ) to list all objects in the current workspace.
ls()
The working directory is a folder that R uses to read files and save results. We can use the function getwd( ) to view the current working directory, and we can also use the function setwd( ) to set the current working directory. Saving all files for an analysis project in one folder makes project management more convenient and improves analysis efficiency. Therefore, on the first line of a code script file, you can usually set the working directory first.
getwd() # Get the working directory path
setwd("/home/project/myprojects/project1") # Change the working directory path
# To save the current workspace to a specified file, enter the following when exiting
save.image("MyFile.Rdata")
# After output, you can refresh the file tree on the right, right-click to copy the file path, or download it
Next time, we only need to use the function load( ) to load the saved workspace and continue the analysis work for this project. When the workload is large, this can greatly improve work efficiency.
If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: