Introduction and introduction to R language

Articles and codes have been archived in [Github warehouse: https://github.com/timerring/dive-into-AI ] or the public account [AIShareLab] can also be obtained by replying to R language .

Common Arithmetic Operators

operator describe
+ addition
subtraction
* multiplication
/ division
^ or ** exponentiation
%% Surplus
%/% 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, reserved digits)
  • ceiling(x)
  • floor(x)
  • trunc(x): intercept the integer part of x
  • Most functions in R packages come with examples, and the function example() is used to run the example code.

R object

In R, "everything is an object". Data analysis includes many steps, from data collation, exploration, modeling to visualization, and each step needs to deal with different objects, such as vectors, matrices, functions, models, etc.

a = 3 + 5
# 可以写成
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
# 也可以在左边计算它的值,然后通过右赋值“->”把结果赋给一个新的对象;这个写法并不常见
a + 3*b -> c
c

Object names can consist of one or more characters. The object name is general 只能以字母开头and can contain numbers, dots "." and underscores "_".

Commonly used relational and logical operators

  • >
  • <
  • ==
  • !=
  • >=
  • <=
  • &
  • |
  • !

workspace management

The workspace is the working environment of R, and all created objects are temporarily stored 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 where R reads files and saves 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. Storing all files of an analysis project in one folder will bring convenience to project management and improve analysis efficiency. Therefore, in the first line of a code script file, you can usually set the working directory first .

getwd()  # 获取工作目录路径
setwd("/home/project/myprojects/project1")  # 修改工作目录路径

# 想要把当前工作空间保存到一个指定的文件,可以在退出时输入
save.image("MyFile.Rdata")
# 输出后可以刷新右侧文件树,右键复制文件路径,或者下载

Next time we load( )can just use the function to load the saved workspace and continue working on the analysis for this project. When the workload is large, the work efficiency can be greatly improved.

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/132352925