# Examining Data Frames -- Head and Tail

head and tail, for those familiar with the unix command line, are two very handy utilities for looking at data frames. Along with str, which displays the structure of a data frame, they help you look at your data:

`

``````> d <- data.frame(mean=rep(1:10,5), val = rnorm(n=50, mean=rep(1:10,5)))
> d <- d[ order(d\$mean), ]
>
> str(d)
'data.frame':   50 obs. of  2 variables:
\$ mean: int  1 1 1 1 1 2 2 2 2 2 ...
\$ val : num  2.303 2.222 -1.153 1.795 -0.232 ...
>
mean        val
1     1  2.3026422
11    1  2.2216371
21    1 -1.1533163
31    1  1.7945563
41    1 -0.2318763
2     2 -0.4994239
>
> tail(d)
mean       val
49    9  8.462525
10   10 10.437314
20   10 10.815264
30   10 10.218853
40   10  9.754245
50   10  9.596825
>
``````

`

If you are familiar with data frames, you’ll know that head(d) is no different than displaying the first 6 rows via subsetting, eg, d[1:6, ] but it saves some typing. tail, on the other hand, saves us from either asking first how many rows a data frame has with nrow, or typing a mess: d[ (nrow(d)-5):nrow(d), ] `

``````> nrow(d)
[1] 50
> d[45:50,]
mean       val
49    9  8.462525
10   10 10.437314
20   10 10.815264
30   10 10.218853
40   10  9.754245
50   10  9.596825
> d[ (nrow(d)-5):nrow(d), ]
mean       val
49    9  8.462525
10   10 10.437314
20   10 10.815264
30   10 10.218853
40   10  9.754245
50   10  9.596825
>
``````