数据类型
数字
逻辑
⽂本
#
数值型向量
a<-c(3,2,6,7,9)
#
字符型向量
b<-c("张","王","李","赵","周")
#
逻辑向量
c<-c("T","F","T","F","F")
cat("数值:",a,"","字符:",b,"","逻辑:",c,"")
## 数值: 3 2 6 7 9
## 字符: 张 王 李 赵 周
## 逻辑: T F T F F
对象类型
向量(vector)
列表(list)
矩阵(matrix)
数组(array)
因⼦(factor)
数据框(data.frame)
向量
v1<-1:7
v2<-seq(1,7,1)
v3<-rep(1:3,times=3)
v4<-rep(1:3,each=3)
v1;v2;v3;v4
列表
list(name = "张三",sex = "男",work = F,num = rep(1:3,each=3))
## $name
## [1] "张三"
##
## $sex
## [1] "男"
##
## $work
## [1] FALSE
##
## $num
## [1] 1 1 1 2 2 2 3 3 3
矩阵
mat<-matrix(data = 1:9,nrow = 3,ncol = 3,byrow = T)
rownames(mat)<-c("甲","⼄","丙")
colnames(mat)<-c("A","B","C")
mat
## A B C
## 甲 1 2 3
## ⼄ 4 5 6
## 丙 7 8 9
## 甲 ⼄ 丙
## A 1 4 7
## B 2 5 8
## C 3 6 9
数组
array(data = c(1:24),dim = c(2,3,4))
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
## [2,] 8 10 12
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 13 15 17
## [2,] 14 16 18
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 19 21 23
## [2,] 20 22 24
因⼦
x <- c("男", "⼥", "男", "男", "⼥")
sex <- factor(x)
sex
## [1] 男 ⼥ 男 男 ⼥
## Levels: 男 ⼥
数据框
names<-c("张三","李四","王六")
stat<-c(70,90,76)
math<-c(84,89,79)
data.frame(姓名=names,语⽂=stat,数学=math)
## 姓名 语⽂ 数学
## 1 张三 70 84
## 2 李四 90 89
## 3 王六 76 79
常⽤函数
舍⼊:round(x = pi,digits = 3)digits⼩数点后保留位数
绝对值:abs(x)
平⽅根:sqrt(x)
a1<-round(x = pi,digits = 3)
a2<-abs(-1)
a3<-sqrt(4)
cat("a1:",a1,"","a2:",a2,"","a3:",a3,"")
## a1: 3.142
## a2: 1
## a3: 2
排序函数: sort(x)返回排序结果 rev(x)返回把各元素排列次序反转后的结果 order(x)返回排序⽤的下标
x <- c(11,33,22,44,55)
sort(x)
统计函数: sum(求和) mean(求平均值) var(求样本⽅差) sd(求样本标准差) min(求最⼩值) max(求最⼤值) range(求最⼩值和最⼤值)
names<-c("张三","李四","王六","李四")
stat<-c(70,90,76,98)
math<-c(84,89,79,99)
d<-data.frame(姓名=names,语⽂=stat,数学=math)
sum(d$语⽂)
取⼦字符串函数: substr(x, start, stop)从字符串x中取出从第start个到第stop个的⼦串
y<-"lkahsdflka"
substr(x = y,start = 2,stop = 4)
时间
year()取出年
month()取出⽉份数值
mday()取出⽇数值
yday()取出⽇期在⼀年中的序号,元旦为1
wday()取出⽇期在⼀个星期内的序号, 但是⼀个星期从星期天开始, 星期天为1,星期⼀为2,星期六为7。
hour()取出⼩时
minute()取出分钟
second()取出秒
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
⾃定义函数
fun1 <- function(x){ -x+1 }
fun2 <- function(x){ (x-2)^2 }
fun3<-function(x){ x+1 }
fun1(c(1,2,3,4,5))
可视化
简单图形
cex:字符或者形状⼤⼩,表示绘图符号相对于默认⼤⼩的缩放倍数。
col:颜⾊
pch:点的形状
lty:线形
lwd:线粗细
par:设定图像布局
adj:设置⽂本和标题的对⻬⽅式
font:字体
las:坐标轴标签样式
data(mtcars)
par(mfrow=c(1,1))
plot(x=mtcars$mpg,y=mtcars$disp,
type="p",
main="主标题",
xlab="x轴",
ylab="y轴",
cex=2,
pch=19,
col='orange',
font = 1,
panel.first = grid(8,8,col='blue',lty=2),family='STKaiti',
frame.plot = F)
curve(fun1,1,10, xlim = c(-10,15), ylim = c(-10,50),lty=2,col = "blue",axes = F,xlab="",ylab="")
curve(fun1,-10,1,col="blue",add = T)
curve(fun3,-10,1,col = "red",add = T)
curve(fun3,1,10,lty=2,col = "red",add = T)
curve(fun2,-10,1,lty=2,col="black",add = T)
curve(fun2,1,15,col = "black",add = T)
text(c(-5,5,-5,-5),c(10,20,-6,2),c(expression(-x+1), expression((x-2)^2),expression(x+1),"y=1"))
title("2022-Math")
abline(a = 0,b = 0,h = 0,v = 2,lty=2,col="darkgray")
abline(h = 1,col="green",lty = 2)
arrows(0,-11,0,50)
arrows(-11,0,16,0)
text(x = 1,y = 40,"Y")
text(x = 14,y = 2,"X")
text(x = 1,y = -3,"(0,0)")
动态图形
library(pacman)
p_load(echarts4r,tidyverse,dplyr)
柱状图、折线图和⾯积图
df <- data.frame(
x = seq(50),
y = rnorm(50, 10, 3),
z = rnorm(50, 11, 2),
w = rnorm(50, 9, 2)
)
df %>%
e_charts(x) %>%
e_bar(y)%>%
e_line(z) %>%
e_area(w) %>%
e_title("Line and area charts")%>%
e_theme("green")
条形图与阶梯图
mtcars %>%
rownames_to_column() %>%
e_charts(rowname) %>%
e_bar(mpg) %>%
e_labels()%>%
e_step(drat) %>%
e_title("条形图与阶梯图")%>%
e_theme("dark")
mtcars %>%
mutate(
model = row.names(.),
total = mpg + qsec
) %>%
arrange(desc(total)) %>%
e_charts(model) %>%
e_bar(mpg, stack = "grp") %>%
e_bar(qsec, stack = "grp") %>%
e_labels()
箱型图
df <- data.frame(
x = c(1:10, 25),
y = c(1:10, -6)
)
df %>%
e_charts() %>%
e_boxplot(y, outliers = TRUE) %>%
e_boxplot(x, outliers = TRUE)%>%
e_labels()
散点图
mtcars %>%
rownames_to_column() %>%
e_charts(rowname) %>%
e_scatter(mpg,drat) %>%
e_visual_map(drat,scale = e_scale) %>%
e_title("散点图")
mtcars %>%
rownames_to_column() %>%
e_charts(rowname) %>%
e_effect_scatter(mpg,drat) %>%
e_visual_map(drat) %>%
e_legend(FALSE)
极坐标图
data <- tibble(
x = seq(50),
y = rnorm(50,10,3)
)
data %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis(x) %>%
e_radius_axis() %>%
e_bar(y,coord_system = "polar")
环状条形图
data %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis() %>%
e_radius_axis(x) %>%
e_bar(y,coord_system = "polar")
漏⽃图
funne <- data.frame(x = c("View1", "Click1", "Purchase1"), value = c(70, 50, 20))
funne %>%
e_charts() %>%
e_funnel(value, x) %>%
e_title("Funnel")
评论0