第4行的data.frame的如何選擇第4行的data.frame
的:選擇R中
Weight Response
1 Control 59 0.0
2 Treatment 90 0.8
3 Treatment 47 0.1
4 Treamment 106 0.1
5 Control 85 0.7
6 Treatment 73 0.6
7 Control 61 0.2
第4行的data.frame的如何選擇第4行的data.frame
的:選擇R中
Weight Response
1 Control 59 0.0
2 Treatment 90 0.8
3 Treatment 47 0.1
4 Treamment 106 0.1
5 Control 85 0.7
6 Treatment 73 0.6
7 Control 61 0.2
使用head
:
dnow <- data.frame(x=rnorm(100), y=runif(100))
head(dnow,4) ## default is 6
使用索引:
df[1:4,]
凡在括號內的值可以解釋爲邏輯,數字或字符(與各個名稱匹配):
df[row.index, column.index]
閱讀幫助(`[``for more關於這個問題的細節,以及在引言閱讀index matrices到R.
對於以數據幀一個可以簡單地輸入
head(data, num=10L)
拿到第10例。
對於data.frame一個可以簡單地輸入
head(data, 10)
獲得第一10.
如果有小於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(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
希望得到這個幫助!
如果有人願意dplyr
的解決方案,這是非常直觀的:
dt <- dt %>%
slice(1:4)
,如果你想只從一個列的第一個四行這也適用。要獲得前四個響應值:'df [1:4,「響應」]'。 – 2014-03-14 01:04:57