如果您的行数少于 4 行,则可以使用该head
函数 (head(data, 4)
或head(data, n=4)
),它就像一个魅力。但是,假设我们有以下 15 行的数据集
>data <- data <- read.csv("./data.csv", sep = ";", header=TRUE)
>data
LungCap Age Height Smoke Gender Caesarean
1 6.475 6 62.1 no male no
2 10.125 18 74.7 yes female no
3 9.550 16 69.7 no female yes
4 11.125 14 71.0 no male no
5 4.800 5 56.9 no male no
6 6.225 11 58.7 no female no
7 4.950 8 63.3 no male yes
8 7.325 11 70.4 no male no
9 8.875 15 70.5 no male no
10 6.800 11 59.2 no male no
11 6.900 12 59.3 no male no
12 6.100 13 59.4 no male no
13 6.110 14 59.5 no male no
14 6.120 15 59.6 no male no
15 6.130 16 59.7 no male no
假设您要选择前 10 行。最简单的方法是data[1:10, ]
.
> data[1:10,]
LungCap Age Height Smoke Gender Caesarean
1 6.475 6 62.1 no male no
2 10.125 18 74.7 yes female no
3 9.550 16 69.7 no female yes
4 11.125 14 71.0 no male no
5 4.800 5 56.9 no male no
6 6.225 11 58.7 no female no
7 4.950 8 63.3 no male yes
8 7.325 11 70.4 no male no
9 8.875 15 70.5 no male no
10 6.800 11 59.2 no male no
但是,假设您尝试检索前 19 行并查看会发生什么 - 您将缺少值
> data[1:19,]
LungCap Age Height Smoke Gender Caesarean
1 6.475 6 62.1 no male no
2 10.125 18 74.7 yes female no
3 9.550 16 69.7 no female yes
4 11.125 14 71.0 no male no
5 4.800 5 56.9 no male no
6 6.225 11 58.7 no female no
7 4.950 8 63.3 no male yes
8 7.325 11 70.4 no male no
9 8.875 15 70.5 no male no
10 6.800 11 59.2 no male no
11 6.900 12 59.3 no male no
12 6.100 13 59.4 no male no
13 6.110 14 59.5 no male no
14 6.120 15 59.6 no male no
15 6.130 16 59.7 no male no
NA NA NA NA <NA> <NA> <NA>
NA.1 NA NA NA <NA> <NA> <NA>
NA.2 NA NA NA <NA> <NA> <NA>
NA.3 NA NA NA <NA> <NA> <NA>
并使用 head() 函数,
> head(data, 19) # or head(data, n=19)
LungCap Age Height Smoke Gender Caesarean
1 6.475 6 62.1 no male no
2 10.125 18 74.7 yes female no
3 9.550 16 69.7 no female yes
4 11.125 14 71.0 no male no
5 4.800 5 56.9 no male no
6 6.225 11 58.7 no female no
7 4.950 8 63.3 no male yes
8 7.325 11 70.4 no male no
9 8.875 15 70.5 no male no
10 6.800 11 59.2 no male no
11 6.900 12 59.3 no male no
12 6.100 13 59.4 no male no
13 6.110 14 59.5 no male no
14 6.120 15 59.6 no male no
15 6.130 16 59.7 no male no
希望这有帮助!