文章目录

  • 第1章 R语言概述
    • 1.选择题
    • 2.操作题
  • 第2章 数据对象与数据读写
    • 1.选择题
    • 2.操作题
  • 第3章 数据集基本处理
    • 1.选择题
    • 2.操作题
  • 第4章 函数与控制流
    • 1.选择题
    • 2.操作题
  • 第5章 初级绘图
    • 1.选择题
    • 2.操作题
  • 第6章 高级绘图
    • 1.选择题
    • 2.操作题
  • 第7章 可视化数据挖掘工具Rattle
    • 1.选择题
    • 2.操作题
  • 资源
  • Reference

第1章 R语言概述

1.选择题

(1)多行注释的快捷键是(C)。
A.Ctrl+Shin+N
B.Ctrl+N
C.Ctrl+Shin+C
D.Ctrl+C

(2)以下函数不能直接查看plot函数的帮助文档的是(B)。
A. ?plot
B.??plot
C.help(plot)
D.help(plot)

(3)以下R包的加载方式正确的是(A)。
A.install.package 函数
B.library 函数
C…libPaths 函数
D.install 函数

(4)以下R包中不能调用分类算法的是(D)。
A.nnet包
B.e1071包
C.tree包
D.arules包

2.操作题

(1)依据1.1节的R下载及安装方法,在计算机上安装R,通过熟悉基本操作的命令及操作界面,掌握软件的使用方法。
(2)依据1.2节的RStudio下载及安装方法,在计算机上安装RStudio,并尝试通过帮助文档学习使用plot函数绘制简单的散点图。
(3)依据1.3节的R包下载及安装方法,在计算机上安装DT包(用于创建交互式表格),并在命令运行窗口运行命令 datatable(iris),将得到交互式表格,如图1-29所示。

图1-29 iris数据集的交互式表格
(4)依据1.4节内容,加载boot包中的acme数据集,并查看acme数据集的前6项。同时,通过help函数查看acme数据集的数据含义并进行说明。

第2章 数据对象与数据读写

1.选择题

(1)下列可以判别字符型数据的函数是(A)。
A. is.numeric
B. is.logical
C. is.character
D.is.na
(2)下列可以判别数值型数据的函数是(D)。
A. is.complex
B. is.na
C.is.integer
D. is.numeric

(3)可将对象转换为逻辑型数据的函数是(D)。
A. as.character
B. is.numeric
C.as.logica
D. as.complex

(4)下列选项不是逻辑型数据的是(C)。
A.T
B.F
C.NA
D.10

(5)下列可以求矩阵的特征值和特征向量的函数是(B)。
A. diag
B. eigen
C.solve
D. det

(6)下列选项中可以使得列表转换为向量的是(D)。
A. as.matrix
B. as.data.frame
C. as.list
D. unlist

(7)下列用来转换数据框的函数是(B)。
A. as.list
B. as.matrix
C. as.data.frame
D. as.vector

(8)下列用键盘导人数据的函数是(B)。
A.read.table
B. read.csv
C.edit
D.readHTMLTable

(9)RODBC包中向数据库提交一个查询,并返回结果的函数是(B)。
A.odbcConnect
B.sqlFetch
C. sqlQuery
D. sqlDrop

(10)抓取网页上的表格,可使用XML包的是(D)函数。
A.read.csv
B. read.table
C.read.xlsx
D. read HTMLTable

2.操作题

(1)创建一个对象,并进行数据类型的转换、判别等操作,步骤如下:
①创建一个对象x,内含元素为序列:1,3,5,6,8
②判断对象x是否是数值型数据
③将对象转换为逻辑数据,记为x1
④判断x1是否为逻辑型数据

#操作题(1)
x<-c(1,3,5,6,8)   #创建一个对象x
is.numeric(x)    #判断x是否是数值型数据
x1<-as.logical(x)  #将对象转化为逻辑型数据
is.logical(x1)  #判断是否为逻辑型数据

(2)创建多种数据结构,并进行数据结构的转换、索引、扩展等编辑操作,步骤如下:
①设置工作空间目录
②创建一个向量x,内含元素为序列:11,23,25,46,38,30,59,47,21,67
③查询向量x中序号为23和46的元素,查询向量x中大于等于50的元素的位置。
④创建一个重复因子序列Species:水平数为3,各水平重复两次,序列长度为5;3个水平为setosa、versicolor、virginica
⑤创建一个5行2列的矩阵,元素为向量x,按列填充
⑥将矩阵写入数据框data_iris,更改列名为Sepal.Length、Sepal.Width
⑦将数据框data_iris保存为TXT文件,保存到工作空间的test目录下
⑧将数据框data_iris转换为向量y
⑨判断是否转换成功

#操作题(2)
setwd("./第2章 数据对象与数据读写/02-习题程序/code")                    #设置工作科技目录
x<-c(11,23,25,46,38,30,59,47,21,67)     #创建向量x
x[c(2,4)]                           #查询向量中第2,第4位置的元素
which(x>35& x<=50)                #查询x中大于35小于等于50元素的位置
#创建一个重复因子序列
Species<-rep(c("setosa","versicolor","virginica"),each=2,length.out=5)   a<-matrix(x,5,2)                    #创建一个元素为x的矩阵
#将矩阵写入数据框data_iris并更改列名
data_iris<-data.frame(Sepal.Length=a[,1],Sepal.Width=a[,2])
write.table(data_iris," ./data_iris.txt")  #将数据框保存为txt文件
#将数据框转化为向量y
b<-as.matrix(data_iris)
y<-as.vector(b)
is.vector(y)   #判断是否为向量

(3)读取TXT文件,进行编辑操作,再写入另外一个CSV文件中,步骤如下:
①读取保存再test目录下的TXT文件data_iris
②将R的示例数据集iris中的第6~10行写入数据框data_iris1中
③将数据框data_iris与data_iris1合并为数据框data_iris2,并保存在CSV文件所在的目录下

#操作题(3)
read.table("./第2章 数据对象与数据读写/02-习题程序/code/data_iris.txt")  #读取txt文件data_iris
data_iris1<-data.frame(iris[6:10,])     #将数据集iris中第6到10行写入数据框
#将数据框data_iris和data_iris1合并为data_iris2
data_iris2<-cbind(data_iris,data_iris1)
#将数据框data_iris2保存为csv文件
write.csv(data_iris2,"./第2章 数据对象与数据读写/02-习题程序/code/data_iris2.csv")

第3章 数据集基本处理

1.选择题

(1)下列不属于用于修改变量名的函数是(C)。
A. rename 函数
B.names 函数
C.name函数
D.colnames 函数

(2)下列用于修改矩阵变量名的函数是(B)。
A.rename函数
B.colnames 函数
C.names 函数
D.name函数

(3)下列属于as.Date函数功能的选项是(A)。
A.将字符串形式的日期值转换为日期变量
B.返回系统当前的日期
C.将字符申转换为包含时间及时区的日期变量
D.将日期变量转换成指定格式的字符型变量

(4)下列关于合并数据集不正确的选项是(C)。
A.数据框的合并可以通过rbind函数和cbind函数
B.rbind函数的自变量的宽度(列数)应该与原数据框的宽度相等
C.rbind函数的自变量的高度(行数)应该与原数据框的宽度相等
D.cbind函数的自变量的高度(行数)应该与原数据框的高度相等

(5)下列不属于sample函数功能的是(B)。
A.放回随机抽样
B.函数排序
C.可对数据进行随机分组
D.不放回随机抽样

(6)下列关于subset函数表达的错误选项是(D)。
A.可用来选取变量与观测变量
B.其中x是所要选择的数据框
C.subset是所要查看信息的方法
D.select查看的某个区域可以大于数据框x

(7)使用merge函数合并数据时,下列为默认值的是(B)。
A.相同列名的列
B.相同行名的行
C.第1列数据
D.第1行数据

(8)使用mell函数操作n维数组时,返回的结果有(A)列。
D.n+2
A.n
B.n+1
C.n-1

(9)元字符·的含义为(B)。
A.前面的字符或表达式重复零次或更多次
B.前面的字符或表达式重复一次或更多次
C.前面的字符或表达式重复零次或一次
D.前面的字符或表达式重复零次

(10)下列关于paste函数表达错误的选项是(D)。
A.参数sep表示分隔符,默认为空格
B.参数 collapse不指定值时,返回值是自变量之间通过sep指定的分隔符连接后得到的一个字符型向量
C.参数 collapse指定了特定的值,则自变量连接后的字符型向量会再被连接成一个字符申,之间通过collapse的值分隔
D.设置collapse参数,返回值为字符向量

2.操作题

(1)创建一个矩阵,并使用交互式编辑器修改变量名;创建一个数据框,并在数据框中添加3个新变量,分别为原数据的差、乘积和余数。

#操作题(1)
a<-matrix(1:12,3,4)
fix(a)
a<-c(1,2,3,4)
b<-c(11,22,33,44)
x1<-data.frame(a,b,d=a-b,e=a*b,f=a%%b)
x1

(2)构建一个含有缺失值的数据框,检测该数据框是否含有缺失值并删除包含缺失值的行;创建一个字符串的日期值,分别使用as.Date 函数、as.POSIXIt 函数、strptime 函数转换为日期变量;使用sort函数对score的Chinese列进行从大到小排列,并且把缺失值放在最后。

#操作题(2)
x2<-data.frame(id=c(1,2,3,4),name=c("张三","李四","王五","赵六"),math=c(70,89,NA,80),English=c(86,78,65,92))
anyNA(x2)
na.omit(x2)
dates <- c("10/27/2017", "02/25/2017", "01/14/2017", "07/18/2017", "04/01/2017")# 按照月日年的格式进行转换
(date <- as.Date(dates, "%m/%d/%Y"))
datas1<- c("2017-09-08 11:17:52", "2017-08-07 20:33:02")
# 对字符串形式的日期时间值按照格式进行转换
as.POSIXlt(datas1, tz = "", "%Y-%m-%d %H:%M:%S")
(datas2 <- strptime(datas1, "%Y-%m-%d %H:%M:%S"))
score <- data.frame(student = c("A", "B", "C", "D"), gender = c("M", "M", "F", "F"), math = c(90, 70, 80, 60), Eng = c(88, 78, 69, 98), p1 = c(66, 59, NA, 88))
names(score)[5] = "Chinese"
score
sort(score$Chinese,decreasing=TRUE, na.last=TRUE)

(3)构建一个数据框,并使用两种方法来选取变量;使用sample函数实现放回随机抽样与不放回随机抽样。

#操作题(3)
data<- data.frame(a = c(5.1, 4.9, 4.7), b = c(3.5, 3.0, 3.2), c = c( 1.4, 1.3, 1.5),d = rep(0.2, 3))
newdata <- data[, c(3:4)]
newdata1 <- subset(data, a == "4.9", select = c(b, d))a<-c(11,22,33,44,55,66,77,88,99)
sample(a, 5, replace = TRUE) #放回简单随机抽样
sample(a, 5, replace = FALSE)  # 不放回简单随机抽样

(4)使用SQL语句对文中数据框stuscore进行计算。
①计算每个人的总成绩并排名(要求显示字段:学号、总成绩)。
②计算每个人单科的最高成绩(要求显示字段:学号、课程、最高成绩)。
③列出各门课程成绩最好的学生(要求显示字段:学号、科目、成绩)。
④列出各门课程成绩最差的学生(要求显示字段:学号、科目、成绩)。

#操作题(4)
name <- c(rep("张三", 1, 3), rep("李四", 3))
subject <- c("数学", "语文", "英语", "数学", "语文", "英语")
score <- c(89, 80, 70, 90, 70, 80)
stuid <- c(1, 1, 1, 2, 2, 2)
stuscore <- data.frame(name, subject, score, stuid)
library(sqldf)
# 计算每个人的总成绩并排名(要求显示字段:学号,总成绩)
sqldf("select stuid, sum(score) as allscore from stuscore group by stuid order by allscore")
# 计算每个人单科的最高成绩(要求显示字段: 学号,课程,最高成绩)
sqldf("select stuid, subject, max(score) as maxscore from stuscore group by stuid")
# 列出各门课程成绩最好的学生(要求显示字段: 学号,科目,成绩)
sqldf("select stuid,  subject,max(score) as maxscore from stuscore group by subject order by stuid")
# 列出各门课程成绩最差的学生(要求显示字段:学号,科目,成绩)
sqldf("select stuid,subject, min(score) as minscore from stuscore group by subject order by stuid")

(5)创建一个列表,并使用melt函数将其融合。

data<-list(a=c(11,22,33,44),b=matrix(1:10,nrow=2),c="one,two,three",d=c(TRUE,FALSE))
data
library(reshape2)
melt(data, varnames = c("X", "Y"), value.name = "value", na.rm = FALSE)

(6)构建一个字符型向量,并使用sub函数和 gsub 函数完成字符串替换;使用paste
两数分别返回一个字符型向量和一个字符串。

#操作题(5)
data1<-c("we","are","family","you","good")
sub("good","bad",data1)
gsub("we","student",data1)b<-paste("ab", 1:3, sep = "")
x <- list(a = "st", b = "nd", c = "yw")
y <- list(d = 1, e = 2)
c<-paste(x, y, sep = "-", collapse = "; ")  # 设置collapse参数,连成一个字符串
c

第4章 函数与控制流

1.选择题

(1)下列能返回不小于x的最小整数的数学函数是(C)。
A.trunc
B.floor
C.ceiling
D.mad

(2)下列不属于apply函数使用对象的是(B)。
A.矩阵
B.向量
C.数组
D.数据框

(3)在ifelse(condition,statementl,statement2)语句中,当condition为TRUE时,执行的语句是(A)。
A.statementl
B.statement2
C.statement3
D.statement4

(4)在switch(expression,list)语句中,当list是有名定义、表达式等于变量名时,返回的结果是(B)。
A.列表相应位置的值
B.变量名对应的值
C.NULL”值
D.列表名

(5)下列不属于条件分支语句的数据分析的应用场景的是(B)。
A.if-else 语句
B.for循环语句
C.switch语句
D.ifelse语句

(6)下列关于cal(exprl,expr2…)函数表达不正确的是(A)。
A.exprl、expr2为需要输出的内容,必须为字符串
B.若exprl为“name”,则输出字符申“name
C.若exprl为变量name,则输出name的值
D.符号“ln”表示换行,表示“ln”后的语句在下一行输出

(7)能让while((i<=10){expr}语句停止循环的选项是(B)。
A.i == 10
B.i == 11
C.i == 5
D.i == 1

(8)使用自定义函数时可通过(A)调用。
A.source 函数
B.var 函数
C.range函数
D.signif函数

(9)函数体不包括(C)部分。
A.异常处理
B.返回值
C.输入值
D.运算过程

(10)下列选项中表示返回值的函数是(D)。
A.median
B.dnorm
C.source
D.retum

2.操作题

(1)使用apply函数族中的函数计算列表x<-list(a=1:5,b=exp(0:3))中的各子列表的最大值、最小值与中位数。

#操作题(1)
x <- list(a = 1:5, b = exp(0:3))
lapply(x, max)
lapply(x, min)
lapply(x, median)

(2)在区间[-5,5上绘制标准正态曲线,求位于z左侧的标准正态曲线下方的面积。

#操作题(2)
#在区间[-5,5]上绘制标准正态曲线
x <- pretty(c(-5,5), 50)
y <- dnorm(x)##密度函数
plot(x, y, type = 'l', xlab = 'Normal Deviate', ylab = 'Density', yaxs = 'i')
#位于z=1.96左侧的标准正态曲线下方的面积
pnorm(1.96)

(3)用条件分支语句将成绩划分为5个等级:A(大于等于90)、B(大于等于80)、(大于等于70)、D(大于等于60)、E(小于60).例如,对成绩87分进行判断。

#操作题(3)
x <- sample(0:100, 1)#随机产生一个0-100的成绩
if(x>=90){grade <- '成绩等级:A'
}else if(x>=80){grade <- '成绩等级:B'
}else if(x>=70){grade <- '成绩等级:C'
}else if(x>=60){grade <- '成绩等级:D'
}else{grade <- '成绩等级:E'
}
grade

(4)判断101-200之间有多少个素数,并输出所有素数。

#操作题(4)
x <- 101:200
y <- matrix(nrow = 200, ncol = 100)
count <- 0
c <- NULL
for(i in 1:100){for(j in 1:200){y[j,i] <- x[i] %% j}if(length(which(y[,i]==0))<=2){count <- count + 1c[count] <- i}
}
count#计数
x[c]#所有素数

(5)编写一个自定义函数求两个矩阵的乘积,并找出乘积矩阵中的最大元素。

#操作题(5)
POM <-function(x,y)
{maxer <- function(x.){print(max(x.))}m1 <- ncol(x)n <- nrow(y)if(m1!=n){print('error dimension is not siutable')return(0)}m <- nrow(x)n1 <- ncol(y)s <-matrix(0,m,n1)for(i in 1:m)for(j in 1:n1)s[i,j] <- sum(x[i,]*y[,j])maxer(s)return(s)
}
#矩阵s的行数等于矩阵x的行数,s的列数等于y的列数。
#乘积s的第m行第n列的元素等于矩阵x的第m行的元素与矩阵y的第n列对应元素乘积之和。
x <-matrix(c(1:6),2,3,byrow = TRUE)#按行
y <-matrix(c(1:6),3,2,byrow = FALSE)#按列
POM(x,y)

第5章 初级绘图

1.选择题

(1)下列可用于绘制散点图的是(A)。
A.plot函数
B.barplot函数
C.boxplot函数
D.hist函数

(2)下列图形中,不能分析数据分布情况的是(C)。
A.散点图
B.直方图
C.多变量相关矩阵图
D.箱线图

(3)数据维度较大时,为比较两两变量之间的相关关系,可以考虑绘制的图形是(D)。
A.散点图
B.箱线图
C.饼图
D.多变量相关矩阵图

(4)为展示数据类别的占比情况,可以考虑绘制的图形是(C)。
A.散点图
B.箱线图
D.多变量相关矩阵图
C.饼图

(5)下列绘制的图形与R函数对应关系不正确的是(B)。
A.散点图-plot函数
B.箱线图-barplot函数
C.QQ图-qqplot 函数
D.散点矩阵图-pairs函数

(6)下列选项中,(D)不是R自带的修改颜色函数。
A.ngb函数
B.colors 函数
C.palette 函数
D.brewer.pal函数

(7)能修改点样式的参数是(B)。
A.cex
B.pch
C.lwd
D.font

(8)可以在图形中的任意位置添加文字说明的函数是(B)。
A.title函数
B.text 函数
C.mtext函数
D.main函数

(9)为在现有图形上添加拟合直线,可以考虑的函数是(B)。
A.line函数
B.lines 函数
C.abline 函数
D.ablines函数

(10)下列图形参数与说明的对应关系不正确的是(D)。
A.axes-是否显示坐标轴
B.xlim-x轴的取值范围
C.col-颜色设置
D.font-是否显示标题

2.操作题

(1)表5-23是某银行贷款拖欠率的数据bankloan.
表5-23 银行贷款拖欠率的数据
Link:banklload.csv
①比较有违约与无违约行为特征的人群分布。
②探索不同特征的人群收入与负债的分布情况。
③探索不同特征的人群收入与负债的关系。

#操作题(1)
# 读取数据
bankloan <- read.csv("../bankloan.csv")
summary(bankloan)# 数据预处理,调整数据类型,将年龄、工龄分组
bankloan$age_group <- cut(bankloan$age,breaks = paste0(2:6, 0),include.lowest = TRUE)
bankloan$seniority_group <- cut(bankloan$seniority,breaks = c(0, 1, 3, 5, 10, 15, 20, 30, 40),include.lowest = TRUE)
bankloan$education <- factor(bankloan$education)
bankloan$debt <- bankloan$debt_rate / 100 * bankloan$income
attach(bankloan)
#这里有个报错没解决# 绘制违约与不违约客户的教育的条形图
pal <- RColorBrewer::brewer.pal(8, "Set1")
de_e <- ftable(education, default)
barplot(de_e, col = pal[1:5], beside = TRUE, xlab = "default")
legend("topright", levels(education), pch = 15, col = pal, bty = "n")
text(11, 270, "education:")# 绘制违约与不违约客户的年龄的条形图
de_a <- ftable(age_group, default)
barplot(de_a, col = pal[1:4], beside = TRUE, xlab = "default")
legend("topright", levels(age_group), pch = 15, col = pal, bty = "n")
text(8.8, 204, "age:")# 绘制违约与不违约客户的工龄的条形图
de_s <- ftable(seniority_group, default)
barplot(de_s, col = pal[1:8], beside = TRUE, xlab = "default")
legend("topright", levels(seniority_group), pch = 15, col = pal, bty = "n")
text(15.3, 140, "seniority:")# 绘制违约与不违约客户的年龄、教育和工龄的Cleveland点图
dotchart(de_e, bg = pal[1:5], labels = levels(education))
dotchart(de_a, bg = pal[1:4], labels = levels(age_group))
dotchart(de_s, bg = pal[1:8], labels = levels(seniority_group))# 绘制不同年龄、教育和工龄的客户收入的直方图
set.seed(1234)
norm_income <- rnorm(1000, mean(income), sd(income))
hist(income, freq = FALSE, breaks = 50)
lines(density(income), col = 2)
lines(density(norm_income), lty = 2, col = 3)
legend("topright", c("density", "normal"), lty = 1:2, col = 2:3, bty = "n")# 绘制不同年龄、教育和工龄的客户负债的直方图
norm_debt <- rnorm(1000, mean(debt), sd(debt))
hist(debt, freq = FALSE, breaks = 50)
lines(density(debt), col = 2)
lines(density(norm_debt), lty = 2, col = 3)
legend("topright", c("density", "normal"), lty = 1:2, col = 2:3, bty = "n")# 绘制不同年龄、教育和工龄的客户收入的核密度图
library(sm)
sm.density.compare(income, factor(education))
legend("topright", levels(education), lty = 1:5, col = 2:6, bty = "n")
text(450, 0.015, "education:")sm.density.compare(income, factor(age_group))
legend("topright", levels(age_group), lty = 1:4, col = 2:5, bty = "n")
text(430, 0.026, "age:")sm.density.compare(income, factor(seniority_group))
legend("topright", levels(seniority_group), lty = 1:8, col = 2:9, bty = "n")
text(425, 0.03, "seniority:")# 绘制不同年龄、教育和工龄的客户负债的核密度图
sm.density.compare(debt, factor(education))
legend("topright", levels(education), lty = 1:5, col = 2:6, bty = "n")
text(40, 0.125, "education:")sm.density.compare(debt, factor(age_group))
legend("topright", levels(age_group), lty = 1:4, col = 2:5, bty = "n")
text(38, 0.16, "age:")sm.density.compare(debt, factor(seniority_group))
legend("topright", levels(seniority_group), lty = 1:8, col = 2:9, bty = "n")
text(37, 0.16, "seniority:")# 绘制不同年龄、教育和工龄的客户收入的箱线图
boxplot(income ~ education, horizontal = TRUE)
boxplot(income ~ age_group, horizontal = TRUE)
boxplot(income ~ seniority_group, horizontal = TRUE)# 绘制不同年龄、教育和工龄的客户负债的箱线图
boxplot(debt ~ education, horizontal = TRUE)
boxplot(debt ~ age_group, horizontal = TRUE)
boxplot(debt ~ seniority_group, horizontal = TRUE)# 根据客户的年龄、教育和工龄对客户收入分组
income_e <- tapply(income, education, function (t) t)
income_a <- tapply(income, age_group, function (t) t)
income_s <- tapply(income, seniority_group, function (t) t)# 绘制不同年龄、教育和工龄的客户收入的小提琴图
library(vioplot)
vioplot(income_e$`1`, income_e$`2`, income_e$`3`, income_e$`4`, income_e$`5`, names = levels(education), border = "black", col = "light green", rectCol = "blue")vioplot(income_a$`[20,30]`, income_a$`(30,40]`, income_a$`(40,50]`, income_a$`(50,60]`, names = levels(age_group), border = "black", col = "light green", rectCol = "blue")vioplot(income_s$`[0,1]`, income_s$`(1,3]`, income_s$`(3,5]`, income_s$`(5,10]`, income_s$`(10,15]`, income_s$`(15,20]`, income_s$`(20,30]`, income_s$`(30,40]`, names = levels(seniority_group), border = "black", col = "light green", rectCol = "blue")# 根据客户的年龄、教育和工龄对客户负债分组
debt_e <- tapply(debt, education, function (t) t)
debt_a <- tapply(debt, age_group, function (t) t)
debt_s <- tapply(debt, seniority_group, function (t) t)# 绘制不同年龄、教育和工龄的客户负债的小提琴图
vioplot(debt_e$`1`, debt_e$`2`, debt_e$`3`, debt_e$`4`, debt_e$`5`, names = levels(education), border = "black", col = "light green", rectCol = "blue")vioplot(debt_a$`[20, 30]`, debt_a$`(30, 40]`, debt_a$`(40, 50]`, debt_a$`(50, 60]`,names = levels(age_group), border = "black", col = "light green", rectCol = "blue")
#这个图报的错我暂时还没找到如何解决vioplot(debt_s$`[0,1]`, debt_s$`(1,3]`, debt_s$`(3,5]`, debt_s$`(5,10]`, debt_s$`(10,15]`, debt_s$`(15,20]`, debt_s$`(20,30]`, debt_s$`(30,40]`, names = levels(seniority_group), border = "black", col = "light green", rectCol = "blue")# 绘制不同年龄、教育和工龄下客户的收入与负债的散点图
le <- levels(education)
op <- par(mfrow = c(1, 5))
for (i in 1:nlevels(education)) {plot(income[education == le[i]], debt[education == le[i]], main = paste("education = ", le[i]), xlab = "income", ylab = "debt")abline(lm(debt[education == le[i]] ~ income[education == le[i]]), col = "red")
}
par(op)la <- levels(age_group)
op <- par(mfrow = c(1, 4))
for (i in 1:nlevels(age_group)) {plot(income[age_group == la[i]], debt[age_group == la[i]], main = paste("age_group = ", la[i]), xlab = "income", ylab = "debt")abline(lm(debt[age_group == la[i]] ~ income[age_group == la[i]]), col = "red")
}
par(op)ls <- levels(seniority_group)
op <- par(mfrow = c(1, 4))
for(i in 1:nlevels(seniority_group)){plot(income[seniority_group == ls[i]], debt[seniority_group == ls[i]], main = paste("seniority_group = ", ls[i]), xlab = "income", ylab = "debt")abline(lm(debt[seniority_group == ls[i]] ~ income[seniority_group == ls[i]]), col = "red")
}
par(op)

(2)根据VADeaths数据集,分别绘制城镇居民与农村居民死亡情况的饼图,添加标题及图例说明,并分析图表。

#操作题(2)
my_sum <- colSums(VADeaths)
Rural <- sum(my_sum[grep('Rural', names(my_sum))])  # 农村居民数据
Urban <- sum(my_sum[grep('Urban', names(my_sum))])  # 城镇居民数据
pie(c(Rural, Urban), labels = c('Rural', 'Urban'), col = c('blue', 'green'))
title('城镇居民与农村居民死亡情况')
legend('topleft', legend = c('Rural', 'Urban'), fill = c('blue', 'green'))

(3)在同一画布上绘制iris数据集的4个属性两两之间的散点图,所得到的结果如图5-36所示。
图5-36 属性两两之间的散点图

#操作题(3)
path = paste0('VADeaths', '.png')
png(filename = path)
pie(c(Rural, Urban), labels = c('Rural', 'Urban'), col = c('blue', 'green'))
title('城镇居民与农村居民死亡情况')
legend('topleft', legend = c('Rural', 'Urban'), fill = c('blue', 'green'))
dev.off()
#这段代码是操作题(2)的,暂时先不改鸢尾花了

(4)将第(3)题的结果保存为PNG文件格式,并储存到当前工作目录下。

#操作题(4)
path = paste('ex5_4','.jpg')
jpeg(file = path)
par(mfrow = c(2,3))
name <- colnames(iris)[1:4]
for (i in 1:3) {for (j in (i+1):4) {plot(iris[, i], iris[, j], xlab = name[i], ylab = name[j])}
}
dev.off()

第6章 高级绘图

1.选择题

(1)下列不能作为lattice包中的绘图函数 formula输人的是(B)。
A.x~y
B.~y
C.x~y|A
D.x~y|A-B

(2)下列绘图函数不属于lattice包的是(C)。
A.xyplot
B.qq
C.qqplot
D.qqmath

(3)lattice包中可实现图形组合的是(C)。
A.par函数
B.layout 函数
C.split参数
D.newpage参数

(4)下列绘图函数与图形对应关系错误的是(A)。
A.histogram-散点图
B.barchar-条形图
C.bwplot-箱线图
D.splom-散点矩阵图

(5)lattice包中的绘图函数的条件变量不能输入(A)。
A.连续型变量
B.离散型变量
C.因子型数据
D.字符型数据

(6)一个图层不包含(D)。
A.data
B.aes
C.mapping
D.geom

(7)下列选项中不能描述坐标系转换关系的是(C)。
A.饼图=堆叠长条图+polar coordinates
B.靶心图=饼图+polar coordinates
C.锯齿图=饼图+polar coordinates
D.锯齿图=柱状图+polar coordinates

(8)ggplot包中实现分面的函数是(D)。
A.par函数
B.layout 函数
C.split参数
D.facet grid函数

(9)下列选项不能描述绘图函数与图形对应关系的是(B)。
A.geom abline-线
B.geom_histogram-条形图
C.geom_boxplot-箱线图
D.geom_point-点

(10)下列不属于图形属性的是(D)。
A.alpha
B.color
C.linetype
D.ncol

2.操作题

(1)表6-7所示是某银行的贷款拖欠率的数据 bankloan.要求使用lattice 包完成以下图形的绘制。
Link:bankloan.csv
①绘制不同年龄、受教育程度和工龄的客户的收入与负债的直方图及密度分布曲线。
②绘制不同年龄、受教育程度和工龄的客户的收入与负债的散点图,并添加回归线。
③绘制不同年龄、受教育程度和工龄的客户违约与否的条形图。
④绘制客户的收人和负债与违约与否的散点图,非添加logistic回归线。
表6-7银行贷款拖欠率数据

#操作题(1)
bankloan = read.csv("./第6章 高级绘图/02-习题程序/code/data/bankloan.csv")
summary(bankloan)
# 数据预处理
bankloan$age_group <- cut(bankloan$age, breaks = paste0(2:6, 0), include.lowest = TRUE)
bankloan$seniority_group <- cut(bankloan$seniority, breaks = c(0, 1, 3, 5, 10, 15, 20, 30, 40), include.lowest = TRUE)
bankloan$education <- factor(bankloan$education)
bankloan$debt <- bankloan$debt_rate / 100 * bankloan$income# lattice
library(lattice)
# histogram of income
# education
histogram_ie <- histogram( ~ income|education, data = bankloan, layout = c(1, 5), nint = 30, type = "count")
densityplot_ie <- densityplot( ~ income, groups = education, data = bankloan, plot.points = FALSE, lty = 1:5, col = 1:5, key = list(title = "education", text = list(levels(bankloan$education)), column = 2, lines = list(lty = 1:5, col = 1:5)))
plot(histogram_ie, position = c(0, 0, 0.5, 1))
plot(densityplot_ie, position = c(0.5, 0, 1, 1), newpage = FALSE)
# age
histogram_ia <- histogram( ~ income|age_group, data = bankloan, layout = c(1, 4), nint = 30, type = "count")
densityplot_ia <- densityplot( ~ income, groups = age_group, data = bankloan, plot.points = FALSE, lty = 1:4, col = 1:4, key = list(title = "age", text = list(levels(bankloan$age_group)), column = 2, lines = list(lty = 1:4, col = 1:4)))
plot(histogram_ia, position = c(0, 0, 0.5, 1))
plot(densityplot_ia, position = c(0.5, 0, 1, 1), newpage = FALSE)# seniority
histogram_is <- histogram( ~ income|seniority_group, data = bankloan, layout = c(1, 8), nint = 30, type = "count")
densityplot_is <- densityplot( ~ income, groups = seniority_group, data = bankloan, plot.points = FALSE, lty = 1:8, col = 1:8, key = list(title = "seniority", text = list(levels(bankloan$seniority_group)), column = 2, lines = list(lty = 1:8, col = 1:8)))
plot(histogram_is, position = c(0, 0, 0.5, 1))
plot(densityplot_is, position = c(0.5, 0, 1, 1), newpage = FALSE)# histogram of debt
# education
histogram_de <- histogram( ~ debt_rate|education, data = bankloan, layout = c(1, 5), nint = 30, type = "count")
densityplot_de <- densityplot( ~ debt_rate, groups = education, data = bankloan, plot.points = FALSE, lty = 1:5, col = 1:5, key = list(title = "education", text = list(levels(bankloan$education)), column = 2, lines = list(lty = 1:5, col = 1:5)))
plot(histogram_de, position = c(0, 0, 0.5, 1))
plot(densityplot_de, position = c(0.5, 0, 1, 1), newpage = FALSE)
# age
histogram_da <- histogram( ~ debt_rate|age_group, data = bankloan, layout = c(1, 4), nint = 30, type = "count")
densityplot_da <- densityplot( ~ debt_rate, groups = age_group, data = bankloan, plot.points = FALSE, lty = 1:4, col = 1:4, key = list(title = "age", text = list(levels(bankloan$age_group)), column = 2, lines = list(lty = 1:4, col = 1:4)))
plot(histogram_da, position = c(0, 0, 0.5, 1))
plot(densityplot_da, position = c(0.5, 0, 1, 1), newpage = FALSE)# seniority
histogram_ds <- histogram( ~ debt_rate|seniority_group, data = bankloan, layout = c(1, 8), nint = 30, type = "count")
densityplot_ds <- densityplot( ~ debt_rate, groups = seniority_group, data = bankloan, plot.points = FALSE, lty = 1:8, col = 1:8, key = list(title = "seniority", text = list(levels(bankloan$seniority_group)), column = 2, lines = list(lty = 1:8, col = 1:8)))
plot(histogram_ds, position = c(0, 0, 0.5, 1))
plot(densityplot_ds, position = c(0.5, 0, 1, 1), newpage = FALSE)
# income vs debt
id_e <- xyplot(debt ~ income|education, data = bankloan, layout = c(1, 5), panel = function(...){panel.lmline(...)panel.xyplot(...)})
id_a <- xyplot(debt ~ income|age_group, data = bankloan, layout = c(1, 4), panel = function(...){panel.lmline(...)panel.xyplot(...)})
id_s <- xyplot(debt ~ income|seniority_group, data = bankloan, layout = c(1, 8), panel = function(...){panel.lmline(...)panel.xyplot(...)})
plot(id_e, split = c(1, 1, 3, 1))
plot(id_a, split = c(2, 1, 3, 1), newpage = FALSE)
plot(id_s, split = c(3, 1, 3, 1), newpage = FALSE)# default vs income or debt_rate
# lattice
glmdei <- as.vector(glm(default ~ income, data = bankloan, family = binomial(link = "logit"))$coefficients)
glmded <- as.vector(glm(default ~ debt_rate, data = bankloan, family = binomial(link = "logit"))$coefficients)paneli <- function (x, y) {panel.curve(exp(glmdei[1] + glmdei[2] * x) / (1 + exp(glmdei[1] + glmdei[2] * x)), col = "red", lwd = 1, lty = 2)panel.xyplot(x, y)
}
paneld <- function (x, y) {panel.curve(exp(glmded[1] + glmded[2] * x) / (1 + exp(glmded[1] + glmded[2] * x)), col = "red", lwd = 1, lty = 2)panel.xyplot(x, y)
}
glmla_dei <- xyplot(default ~ income, data = bankloan, panel = paneli)
glmla_ded <- xyplot(default ~ income, data = bankloan, panel = paneld)
plot(glmla_dei, split = c(1, 1, 2, 1))
plot(glmla_ded, split = c(2, 1, 2, 1), newpage = FALSE)# bar of default
# lattice
tbankloan <- table(bankloan$education, bankloan$seniority_group, bankloan$age_group, bankloan$default)
barchart(tbankloan, auto.key = list(title = "default", columns = 2))

(2)针对(1)中提及的bankloan的示例表,使用ggplo12包完成以下图形的绘制。
①绘制不同年龄、受教育程度和工龄的客户的收入与负债的直方图和密度分布曲线。
②绘制不同年龄、受教育程度和工龄的客户的收人与负债的散点图,并添加回归线。
③绘制不同年龄、受教育程度和工龄的客户违约与否的条形图。
④绘制客户的收人和负债与违约与否的散点图,并添加logistic回归线。

#操作题(2)
bankloan = read.csv("./第6章 高级绘图/02-习题程序/code/data/bankloan.csv")
summary(bankloan)
# 数据预处理
bankloan$age_group <- cut(bankloan$age, breaks = paste0(2:6, 0), include.lowest = TRUE)
bankloan$seniority_group <- cut(bankloan$seniority, breaks = c(0, 1, 3, 5, 10, 15, 20, 30, 40), include.lowest = TRUE)
bankloan$education <- factor(bankloan$education)
bankloan$debt <- bankloan$debt_rate / 100 * bankloan$income
# ggplot2
library(ggplot2)
library(grid)
page <- function (x, y) viewport(layout.pos.row = x, layout.pos.col = y)
# histogram of income
# education
hist_ie <- ggplot(data = bankloan, aes(x = income)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(education ~ .)
density_ie <- ggplot(data = bankloan, aes(x = income, colour = education)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_ie, vp = page(1, 1))
print(density_ie, vp = page(1, 2))
# age
hist_ia <- ggplot(data = bankloan, aes(x = income)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(age_group ~ .)
density_ia <- ggplot(data = bankloan, aes(x = income, colour = age_group)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_ia, vp = page(1, 1))
print(density_ia, vp = page(1, 2))
# seniority
hist_is <- ggplot(data = bankloan, aes(x = income)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(seniority_group ~ .)
density_is <- ggplot(data = bankloan, aes(x = income, colour = seniority_group)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_is, vp = page(1, 1))
print(density_is, vp = page(1, 2))
# histogram of debt
# education
hist_de <- ggplot(data = bankloan, aes(x = debt_rate)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(education ~ .)
density_de <- ggplot(data = bankloan, aes(x = debt_rate, colour = education)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_de, vp = page(1, 1))
print(density_de, vp = page(1, 2))
# age
hist_da <- ggplot(data = bankloan, aes(x = debt_rate)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(age_group ~ .)
density_da <- ggplot(data = bankloan, aes(x = debt_rate, colour = age_group)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_da, vp = page(1, 1))
print(density_da, vp = page(1, 2))
# seniority
hist_ds <- ggplot(data = bankloan, aes(x = debt_rate)) + geom_histogram(bins = 30, fill = "#0080ff") + facet_grid(seniority_group ~ .)
density_ds <- ggplot(data = bankloan, aes(x = debt_rate, colour = seniority_group)) + geom_density() + theme(legend.position = "top")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(hist_ds, vp = page(1, 1))
print(density_ds, vp = page(1, 2))
# income vs debt
ggid_e <- ggplot(data = bankloan, aes(x = income, y = debt)) + geom_point(colour = "#0080ff") + stat_smooth(method = lm, lwd = 0.5, se = FALSE) + facet_grid(education ~ .)
ggid_a <- ggplot(data = bankloan, aes(x = income, y = debt)) + geom_point(colour = "#0080ff") + stat_smooth(method = lm, lwd = 0.5, se = FALSE) + facet_grid(age_group ~ .)
ggid_s <- ggplot(data = bankloan, aes(x = income, y = debt)) + geom_point(colour = "#0080ff") + stat_smooth(method = lm, lwd = 0.5, se = FALSE) + facet_grid(seniority_group ~ .)grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 3)))
print(ggid_e, vp = page(1, 1))
print(ggid_a, vp = page(1, 2))
print(ggid_s, vp = page(1, 3))# default vs income or debt_rate
# ggplot2
glmgg_dei <- ggplot(data = bankloan, aes(x = income, y = default))  + geom_point()  +  stat_smooth(method = glm, method.args = list(family = "binomial"), se = FALSE)
glmgg_ded <- ggplot(data = bankloan, aes(x = debt_rate, y = default))  + geom_point()  +  stat_smooth(method = glm, method.args = list(family = "binomial"), se = FALSE)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(glmgg_dei, vp = page(1, 1))
print(glmgg_ded, vp = page(1, 2))# bar of default
# ggplot2
ggplot(bankloan, aes(x = education, fill = factor(default))) + geom_bar(position = "stack") + facet_grid(age_group ~ seniority_group)

(3)结合(1)与(2)的操作题,用以上两个操作题中所绘制的图形创建脚本ui.R和server.R,利用shiny包搭建数据可视化平台demo.

第7章 可视化数据挖掘工具Rattle

1.选择题

(1)下列不属于Rattle工具功能的是(C)。
A.相关性分析
B.周期性分析
C.数据集成
D.数据变换

(2)Rattle工具不能导入(D)。
A.R包的数据
B.R工作空间的数据
C.R文件的数据
D.R命名的数据

(3)如果想要查看数据总体的概况,那么应该运用(A)功能。
A. Summary
B. Distribution
C.Correlation
D.Principal Components

(4)Rattle的交互图GGobi包不可以实现(D)的综合使用。
A.散点图
B.散点矩阵图
C.三维图
D.星状图

(5)数据建模中,聚类分析不能得到的结果是(C)。
A.聚类的结果
B.聚类分布图
C.聚类的评价
D.聚类样本投影图

(6)关联规则的Apriori算法不会默认设置的是(A)。
A. Basket
B.Support
C.Confidence
D.Min Length

(7)在Model选项中,分类算法需要设置Min Bucket的值,如果要分成70、15、15的占比,则应该设置为(B)。
A.0.7/0.15/0.15
B.70/15/15
C. 7/1.5/1.5
D.70%/15%/15%

(8)Rattle工具的随机森林模型不能得到(D)。
A.ROC曲线
B.重要性Gini指数
C.错误率
D.分类树

(9)下列不属于Rattle工具的模型评估方法的是(B)。
A.混淆矩阵
B.风险评估图
C.敏感度与特异性图
D.累计增益图

(10)下列数据挖掘算法中,适合用Rale工具的是(C)。
A.分类与预测
B.聚类分析
C.智能推荐
D.关联规则

2.操作题

打开Rattle工具的图形界面,导人Telephone.csv数据,并将数据按照70:15:15的比例分成训练集、验证集和测试集。然后对数据进行探索,完成描述性统计分析、图形探索等操作。提示:在Data选项卡中选择合适的变量构建模型,在Model选项卡中选择合适的分类模型,并对模型进行评估。

资源

百度网盘–课后习题答案&源代码&数据
PS:永久有效,自动填充提取码,提取码:1111

Reference

R语言编程基础-图书-人邮教育社区 →\rightarrow→ 49611-R语言编程基础-习题数据和答案.rar

【R语言编程基础】【课后习题答案】【全】相关推荐

  1. 计算机网路基础课后习题答案 主编刘建友

    计算机网路基础课后习题答案 第一章 计算机网络概述 一.填空题 二.单项选择题 第二章 物理层 一.填空题 二.单项选择题 三.简答题 第三章 数据链路层 一.填空题 二.单项选择题 三.简答题 第四 ...

  2. 河南理工大学计算机课d,河南理工大學计算机基础课后习题答案.doc

    河南理工大學计算机基础课后习题答案 习题1 1. 选择题 (1)计算机的软件系统可分为 D . A)程序和数据 B)操作系统和语言处理系统 C)程序.数据和文档 D)系统软件和应用软件 (2) 一个完 ...

  3. 华师计算机应用基础知识,华师计算机应用基础课后习题答案.doc

    华师计算机应用基础课后习题答案 华师计算机应用基础课后习题答案 华师作业答案 2011年<计算机基础>期末考试复习题 第一章 计算机基础知识 选择题: 1. 目前使用最广泛.发展最快的计算 ...

  4. 电大计算机dm编写程序,电大数控编程技术课后习题答案.doc

    文档介绍: 电大数控编程技术课后****题答案 第一章数控加工的编程基础课后****题答案 一.填空题 1.为了准确地判断数控机床的运动方向,特规定永远假设***相对于(静止的工件)坐标而运动. 2. ...

  5. r语言编程基础_这项免费的统计编程课程仅需2个小时即可学习R编程语言基础知识

    r语言编程基础 Learn the R programming language in this course from Barton Poulson of datalab.cc. This is a ...

  6. c语言定义y1代表英里数,河南理工大学C语言第二章课后习题答案.ppt

    河南理工大学C语言第二章课后习题答案 第二章 基本数据类型.运算符与表达式 1.选择题 (1)下面四个选项中,均是合法整型常量的选项是 A. 160 , -0xffff, 011B. -0xcdf, ...

  7. Python快速编程入门课后习题答案

    文章目录 前言 第一章 一.填空题 二.判断题 三.选择题 四.简答题 第二章 一.填空题 二.判断题 三.选择题 四.简答题 第三章 一.填空题 二.判断题 三.选择题 四.简答题 第四章 一.单选 ...

  8. c语言程序设计基础课后习题答案,2011级C语言程序设计基础教程课后习题答案

    1.C 语言课后习题习题答案详解(11 级教材)第 1 章1.1 填空题1.1.1 应用程序 ONEFUNC.C 中只有一个函数,这个函数的名称是 _main .1.1.2 一个函数由_函数头 _和_ ...

  9. 计算机软件基础 一课本,计算机软件基础(-)课后习题答案.doc

    文档介绍: 计算机软件基础(-)课后****题答案.doc第一章一.简答题1.参考书上第五页图1-72.因为C语言是强类型语言,语法规定必须先定义后使用,只有先定义,系统才能为其分配存储空间.3.参考 ...

最新文章

  1. .exp文件_exp及expdp的主要区别及常用的导入导出操作
  2. 《智慧书》格言241-250
  3. JAVA写的爬虫小工具
  4. 牛客 - 仓库选址(中位数+思维)
  5. matlab向量的角标,【MATLAB】利用冒号表达式获得子矩阵
  6. C语言中写一个函数返回参数二进制中 1 的个数
  7. glassfish 是oracle的,GlassFish“百天”小版本 彰显Oracle的大功力
  8. 62. KVOController详解
  9. jsp中的session和上下文
  10. radius认证服务器部署linux,CentOS安装配置radius服务器
  11. 软件工程专业学习路线
  12. java-序列化以及反序列化
  13. STM32驱动步进电机测试
  14. 关于python的自省机制
  15. 计算机插入的u盘文件打不开,为什么u盘文件夹打不开
  16. PTF 安装及简单测试 Packet Testing Framework
  17. JAVA医院预约挂号系统毕业设计 开题报告
  18. 大物下(大学物理知识点回顾与典型题解析
  19. 莫比乌斯圈matlab,神奇的莫比乌斯圈(PPT).ppt
  20. 最佳阵容怎么找不到服务器,最佳阵容新服最佳782服开服时间表_最佳阵容新区开服预告_第一手游网手游开服表...

热门文章

  1. Excel使用技巧之如何修改CSV文件的分隔符
  2. UVa 1471 Defense Lines - 线段树 - 离散化
  3. 怎么在微软官网找到visual studio历史版本
  4. IEEE 1588 Ordinary clocks
  5. 移动端 - Android客户端性能测试常见指标
  6. win7台式计算机怎么连热点,win7热点怎么设置 win7热点设置的具体方法介绍
  7. 交换机Access和Trunk的配置与删除!
  8. o2o模式和b2c模式的不同点是什么?
  9. notepad++插件
  10. 技嘉1080显卡体质测试软件,技嘉GTX1080 Xtreme Gaming显卡评测:创新的重峦式三风扇...