Data Science R language crash

Article Updated: 2020-03-07
By convention, files need to attach a link on the text of the first:

File name: R-3.6.2-win.exe
File Size: 82.4M
Download Link: https://www.lanzous.com/i9c70mf
SHA256: DC967492639B236BA057FA3B4CA482FA64C7A8E4CAD720595592C5387D28B49F

A, R description language

category description
use R Language is a language for data analysis and mapping of the environment, open.
The latest version of R Version 3.6.3 (as of 2020-02-29)
Official website https://www.r-project.org/
Rstudio-IDE https://rstudio.com/

Two, R grammar language

(A) rules

Species or command description
Case Case sensitive
name Name can not begin with numbers
Assignment operator <-Or =(the two are different)
The Notes #
Help documentation help()help(命令)??.start()?命令??字符
R Object VectorsVector, Listslists, Matricesmatrices, Arraysarrays, Factorsfactors, Data Framesdata frame / frame
type of data Logical(逻辑型), Numeric(数字), Integer(整型),
Complex(复合型), Character(字符), Raw(原型)

Logical(TRUE FALSE), Numeric(. 8 9.9), Integer(2L 34L 0L),

Complex(. 3 + 4i), Character( 'A' "Great" "3.5 of"),

Raw( "the Hello" is stored as the 48 65 6c 6c 6f)
Define the variable height <- 980
ls () View current workspace variables, for example: ls ()
rm() Delete the specified variable, for example: rm (height)
to delete all variables: rm (list = ls () )
Arithmetic Operators +(Plus), -(subtraction), *(multiplication), /(division), %%(remainder), %/%(divisible)
Relational Operators ><==>=<=!=
Logical Operators &|!&&||
Colon operator :Vector in order to create a series of numbers
%in% %in% This operator is used to identify whether an element vector.
%*% %*%This operator is used to matrix and its transpose multiplication.
The if statement if(boolean_expression) { }
if-else statement if(boolean_expression) { } else { }
switch statement switch(expression, case1, case2, case3…)
.libPaths() Get the path where the package
library() Get all installed packages
Load package library("package Name", lib.loc = "path to library")
searc() Get all the packages have been loaded
Installation package install.packages("Package Name")Command to retrieve packages from CRAN website and in the R package installation environment.
repeat cycle repeat { commands if(condition) { break }}
while loop while (test_expression){statement}
for 循环 for (test_expression) { statement }
break 用于终止循环
next 用于跳过当前当次循环,和python中的continue功能相同
定义函数 function_name <- function(arg_1, arg_2, ...) { Function body }
字符串 字符串以单引号双引号括住,两种括号可以穿插。

(二)编程实践

1、定义变量

# 下面三种方法具有相同的作用
var1 <- c(4, 5)
var1 = c(4, 5)
c(4, 5) -> var1

# 向量c(TRUE,1)具有逻辑和数值类的混合。 因此,逻辑类强制转换为数字类,使TRUE为1。
var2 <- c(TRUE, 1)

# 在R语言中,变量本身没有声明任何数据类型,而是获取分配给它的R - 对象的数据类型。
# 所以R称为动态类型语言,这意味着我们可以在程序中使用同一个变量时,一次又一次地更改变量的数据类型。 

定义变量

2、算数运算符

算数运算

三、数据结构

1、向量

创建向量的方法:

  1. seq函数 seq(from = x, to = y, length.out = z)
seq(1, 10, by = 2)
# 1 3 5 7 9

seq(1, 10, length.out = 5)
# 1 3 5 7 9

注:这四个量不可同时指定,否则报参数过多错误。

  1. rep 函数 rep(x, times)
rep(3, 4)
# 3 3 3 3

rep(1:3, each =2)
# 1 1 2 2 3 3

访问向量的方法:

  1. 使用索引(注:从1开始)访问向量
myc <- 1:10
cat(myc)
# 1 2 3 4 5 6 7 8 9 10
myc[c(1, 5, 7, 19)]
# 1 5 7 NA

注1:如果使用负数做索引,则表示不显示这个位置的值。
注2:如果使用布尔值做索引,则显示True 位置的值。

向量的修改和运算:

  1. 使用 append 添加元素到向量
myc <- 1:4
append(myc, 'a', after = 3)
# "1", "2", "3", "a", "4"
  1. 向量的相加
    如果两个向量长度一样,对应相加,如果长度不一样,则短的循环。

  2. 向量的比较
    和向量的相加类似。
    如果长度一样,对应位置相比。
    如果长度不一样,短的循环。

返回的结果都是布尔值向量。

# myc中是否有大于3的值
any(myc > 3)

# myc中是否全都大于3
all(myc >3)

2、矩阵和数组

创建矩阵的方法:

  1. 使用 dim()函数
# 创建一个2行5列的矩阵
y <- 1:10
dim(y) <- c(2, 5);
  1. 使用 matrix() 函数
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
matrix(c(1, 2, 3, 11, 12, 13), nrow = 2, byrow = TRUE, dimnames = list(c("row1", "row2"), c("c.1", "c.2", "c.3")))
  1. 使用 rbind() cbind() 函数
# 按行进行 拼接
mat1 <- rbind(A = 1:3, B = 4:6); 

# 使用矩阵按列进行拼接
mat2 <- cbind(mat1, cbind(c(11, 12), c(13, 14)))

矩阵的运算:

  1. 矩阵乘法使用 %*%
    第一个矩阵的行乘以第二个矩阵的列之和,放在结果的对应位置

矩阵相乘
如果使用 c(1,2)mat1 相乘,因为前者不够数,所以会进行按列重复填充后与后者相乘。

  1. 矩阵的求逆
发布了75 篇原创文章 · 获赞 8 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_21516633/article/details/104685842