head and tail functions in R

head and tails function in R used to get first or last parts of a vector,table, matrix, data frame or function.

head() function: Used to get first parts of vector,table, matrix, data frame or function. By default, it will provide first 6 rows.
tail() function: Used to get last parts of vector,table, matrix, data frame or function. By default, it will provide last 6 rows.

Examples for head function in R:

> require(ggplot2) #load the package ggplot2
Loading required package: ggplot2
Warning message:
package ‘ggplot2’ was built under R version 3.3.3
> data(“diamonds”)
> head(diamonds) # This provides first 6 rows(default)

# A tibble: 6 x 10 carat cut color clarity depth table price x y z 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48

> head(diamonds,10)

# A tibble: 10 x 10 carat cut color clarity depth table price x y z 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 10 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39 >

Examples for head function in R:
> require(ggplot2) #load the package ggplot2
Loading required package: ggplot2
Warning message:
package ‘ggplot2’ was built under R version 3.3.3
> data(“diamonds”)
> tail(diamonds)# This provides last 6 rows(default)

# A tibble: 6 x 10 carat cut color clarity depth table price x y z 1 0.72 Premium D SI1 62.7 59 2757 5.69 5.73 3.58 2 0.72 Ideal D SI1 60.8 57 2757 5.75 5.76 3.50 3 0.72 Good D SI1 63.1 55 2757 5.69 5.75 3.61 4 0.70 Very Good D SI1 62.8 60 2757 5.66 5.68 3.56 5 0.86 Premium H SI2 61.0 58 2757 6.15 6.12 3.74 6 0.75 Ideal D SI2 62.2 55 2757 5.83 5.87 3.64 > tail(diamonds,10)

# A tibble: 10 x 10 carat cut color clarity depth table price x y z 1 0.71 Premium E SI1 60.5 55 2756 5.79 5.74 3.49 2 0.71 Premium F SI1 59.8 62 2756 5.74 5.73 3.43 3 0.70 Very Good E VS2 60.5 59 2757 5.71 5.76 3.47 4 0.70 Very Good E VS2 61.2 59 2757 5.69 5.72 3.49 5 0.72 Premium D SI1 62.7 59 2757 5.69 5.73 3.58 6 0.72 Ideal D SI1 60.8 57 2757 5.75 5.76 3.50 7 0.72 Good D SI1 63.1 55 2757 5.69 5.75 3.61 8 0.70 Very Good D SI1 62.8 60 2757 5.66 5.68 3.56 9 0.86 Premium H SI2 61.0 58 2757 6.15 6.12 3.74 10 0.75 Ideal D SI2 62.2 55 2757 5.83 5.87 3.64

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools
%d bloggers like this: