课程:数据分析技术 实验室名称: 指导老师: 实训日期: 年 月 日 实训题目:R语言语法基础 实训目的:
- 掌握R语言开发环境的搭建。
- 掌握R语言的基本数据类型。
- 掌握R语言常用的数据对象。 实训内容:
- 搭建R开发环境并掌握RStudio的基本用法
- 练习R常用数据结构的用法 实验过程与结果 R开始环境的搭建,参看博客:https://hcshow.blog.csdn.net/article/details/120360145 实验一:创建向量
a <- c(1,2,4,5,8)
b <- c("one","two","three")
c <- c(TRUE,FALSE,TRUE,TRUE,FALSE)
实验二:向量运算
x <- c(1,3,5,7)
y <- c(2,4,6,8)
x+y
x-y
x*y
x/y
x^y
实验三:数组
array(1:24,c(2,3,4))
a <- array(1:24,c(2,3,4))
dim(a)
b <- array(1:16)
b
实验四:列表
a <- c(1,2,3,4,5)
b <- c("nan","nan","nv","nan","nv","nan")
c <- c(TRUE,TRUE,FALSE,FALSE,FALSE)
x <- list(aa=a,bb=b,cc=c)
x
# 访问第一个元素
# 列表的索引
x[[1]]
x$aa
x$bb
实验五:矩阵
matrix(1:30,nrow=6,dimnames = list(c(1:6),c("one","two","three","four","five")))
结果:
实验六:数据框
name <- c("zhangsan","lisi","wanger","mazi")
sex <- c("nan","nv","nv","nv")
age <- c(18,22,33,17)
df <- data.frame(name,sex,age)
df
class(df) # 查看类型
df[1,3]
# 通过索引访问
df$sex # 获取指定列的数据
df$name[2]