To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.
Use head
:
dnow <- data.frame(x=rnorm(100), y=runif(100))
head(dnow,4) ## default is 6
Using the index:
df[1:4,]
Where the values in the parentheses can be interpreted as either logical, numeric, or character (matching the respective names):
df[row.index, column.index]
Read help(`[`) for more detail on this subject, and also read about index matrices in the Introduction to R.
In case someone is interested in dplyr
solution, it's very intuitive:
dt <- dt %>%
slice(1:4)
If you have less than 4 rows, you can use the head
function ( head(data, 4)
or head(data, n=4)
) and it works like a charm. But, assume we have the following dataset with 15 rows
>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
Let's say, you want to select the first 10 rows. The easiest way to do it would be 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
However, let's say you try to retrieve the first 19 rows and see the what happens - you will have missing values
> 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>
and with the head() function,
> 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
Hope this help!
For at DataFrame one can simply type
head(data, num=10L)
to get the first 10 for example.
For a data.frame one can simply type
head(data, 10)
to get the first 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With