1. DataFrame的创建以及随机数,字母的生成
example.data <- data.frame(x = rnorm(100),
y = runif(100),
cat = sample(LETTERS[1:2], prob = c(1, 3), size = 100, replace = TRUE))
这段代码完成了以下操作:
x = rnorm(100)
:生成了100个服从标准正态分布的随机数,作为x
列的数据。y = runif(100)
:生成了100个均匀分布的随机数(范围从0到1),作为y
列的数据。cat = sample(LETTERS[1:2], prob = c(1, 3), size = 100, replace = TRUE)
:生成了100个分类变量的数据,其中类别A
和B
的概率分别是1和3。这里LETTERS[1:2]
表示从字母A和B中随机选择,prob = c(1, 3)
定义了选择A和B的概率比率,size = 100
表示生成100个值,replace = TRUE
表示每次选择后都可以重复。
2. 读取某列并画箱型图:boxplot
boxplot(example.data$x, horizontal = TRUE)
boxplot
用来画箱型图, horizontal可以控制水平还是垂直$
符号用来提取某列
3. 使用ggplot画boxplot
library(ggplot2)
ggplot(example.data, aes(x = x)) + geom_boxplot() + theme_minimal()
library(ggplot2)
:加载ggplot2
包,这个包提供了用于数据可视化的功能。ggplot(example.data, aes(x = x))
:创建一个基础的 ggplot 图形,example.data
是数据集,aes(x = x)
设置了 x 轴的数据来源,即数据框中的x
列。geom_boxplot()
:添加一个箱线图层(box plot),用来展示x
变量的分布情况。theme_minimal()
:应用minimal
主题,使图形背景更加简洁,去除多余的元素。
4. 直方图: hist()
hist(example.data$x)
ggplot(example.data, aes(x = x)) + geom_histogram() + theme_minimal()
5. 柱状图: barplot
barplot(table(example.data$cat))
ggplot(example.data, aes(x = cat)) + geom_bar() + theme_bw()
table(example.data$cat)
:table()
函数会计算 cat
列中每个类别(A
和 B
)的频数。这会返回一个包含每个类别的计数的表格。
假设 cat
列的频数是:
A
类别:25次B
类别:75次
barplot(...)
:barplot()
函数将这个频数表格绘制为柱状图,显示每
6. 散点图
plot(y ~ x, data = example.data, main = "formula input")
plot(example.data$x, example.data$y, main = "argument input")
main
是标题的意思,这两种效果一致
ggplot(example.data, aes(x = x, y = y)) + geom_point()
7. R 语法的特点
1. 赋值符号 (<-
)
在 R 中,<-
符号用于赋值。例如:
x <- 14
这将值 14
赋给变量 x
。
x <- 14
等价于x = 14
,当在命令行输入时使用。- 但是,在函数中传递参数时,应该使用
=
来进行匹配。例如:
2. 句点符号 (.
)
R 中可以使用句点符号 .
作为变量名的一部分。例如:
new.vector <- c("A", "B", "C")
这里 new.vector
就是一个合法的变量名。
3. 元素索引从 1 开始
R 中的向量和数据结构的索引从 1 开始,而不是 0。例如:
new.vector <- c("A", "B", "C")
new.vector[0]
输出:
character(0)
对于索引 1 的值:
new.vector[1]
输出:
[1] "A"
R 中的基本数据类型
1. 因子(Factor)
因子是 R 中用于表示分类数据的一种数据类型,它具有某些附加属性,通常用于处理类别数据。例如:
data("ToothGrowth")
levels(ToothGrowth$supp)
输出:
[1] "OJ" "VC"
ToothGrowth$supp
是一个因子类型的变量,显示了不同的水平(类别)。
2. 经典数据类型
- Numeric:数值型
- Integer:整数型
- Logical:逻辑型(TRUE/FALSE)
- Character:字符型
- Complex:复数型
同质与非同质数据类型
1. 同质数据类型
- 向量(Vector):一组数据元素,所有元素具有相同的数据类型。
- 矩阵(Matrix):一个二维数据结构,具有相同的数据类型。
2. 非同质数据类型
- 列表(List):一个更通用的结构,可以包含不同类型的对象,甚至可以包含其他列表。
- 数据框(Data Frame):用于存储数据的结构,每列可以是不同的基本数据类型,但所有列的长度必须相同。
向量(Vector)
创建一个向量:
new.vector <- c(1, 2, 3)
class(new.vector)
输出:
[1] "numeric"
length(new.vector)
输出:
[1] 3
访问向量的一部分:
new.vector[1:2]
输出:
[1] 1 2
如果混合了不同类型的数据:
new.vector <- c(1, 2, "hello")
class(new.vector)
输出:
[1] "character"
矩阵(Matrix)
创建一个矩阵:
A <- matrix(c(2, 4, 3, 1, 7, 8), nrow = 3)
A
输出:
[,1] [,2]
[1,] 2 1
[2,] 4 7
[3,] 3 8
访问矩阵中的元素:
A[2, 1]
输出:
[1] 4
访问矩阵的某一行:
A[1, ]
输出:
[1] 2 1
列表(List)
创建一个列表:
vector.a <- c(1, 2, 3)
vector.b <- c("hello", "world", "!!")
new.list <- list(vector.a, vector.b)
new.list
输出:
[[1]]
[1] 1 2 3
[[2]]
[1] "hello" "world" "!!"
访问列表中的元素:
new.list[[1]]
输出:
[1] 1 2 3
数据框(Data Frame)
创建一个数据框:
head(warpbreaks)
输出:
breaks wool tension
1 26 A L
2 30 A L
3 54 A L
4 25 A L
5 70 A L
6 52 A L
查看数据框的结构:
str(warpbreaks)
输出:
'data.frame': 54 obs. of 3 variables:
$ breaks : num 26 30 54 25 70 52 51 26 67 18 ...
$ wool : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
$ tension: Factor w/ 3 levels "L","M","H": 1 1 1 1 1 1 1 1 1 2 ...
查看数据框列的名称:
names(warpbreaks)
输出:
[1] "breaks" "wool" "tension"
查看 wool
列的前几项:
head(warpbreaks$wool)
输出:
[1] A A A A A A
Levels: A B