web-dev-qa-db-ja.com

Rのdata.frameの最初の4行を選択します

data.frameの最初の4行を選択するにはどうすればよいですか:

              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
93
Moe

headを使用します。

dnow <- data.frame(x=rnorm(100), y=runif(100))
head(dnow,4) ## default is 6
132
Eduardo Leoni

インデックスの使用:

df[1:4,]

括弧内の値は、論理、数値、または文字(それぞれの名前と一致)として解釈できる場合:

df[row.index, column.index]

このテーマの詳細についてはhelp( `[`)をお読みください。また、Rの概要の インデックスマトリックス についてもお読みください。

109
Shane

誰かがdplyrソリューションに興味がある場合、それは非常に直感的です:

dt <- dt %>%
  slice(1:4)
12
Giacomo

行が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

この助けを願っています!

DataFrameでは、次のように入力できます。

head(data, num=10L)

たとえば、最初の10個を取得します。

Data.frameの場合、単純に入力できます

head(data, 10)

最初の10を取得します。

9
Ole Petersen