How to get first and last rows of Data Frame in R

We can get first n rows of Data Frame using head() function and can get last n rows of Data Frame using tail() function.

Syntax of head() and tail() functions:

head(data.frame,number_of_first_rows) tail(data.frame,number_of_last_rows)

> emp_data emp_id emp_names 1 1 Kalpesh 2 2 George 3 3 David 4 4 Karunakar 5 5 Prem 6 6 Ravi 7 7 Reddy 8 8 Varun 9 9 Michael 10 10 Kevin 11 11 Smith 12 12 Peter 13 13 Tiger 14 14 Travis 15 15 Siva 16 16 Naresh 17 17 Jagan 18 18 Puppesh 19 19 Head 20 20 Mark

> class(emp_data) # To check the data frame or not.
[1] “data.frame”

How to get First 5 Row of Data Frame in R

> head(emp_data,5) emp_id emp_names 1 1 Kalpesh 2 2 George 3 3 David 4 4 Karunakar 5 5 Prem

How to get First 5 Row of Data Frame in R

> head(emp_data,10) emp_id emp_names 1 1 Kalpesh 2 2 George 3 3 David 4 4 Karunakar 5 5 Prem 6 6 Ravi 7 7 Reddy 8 8 Varun 9 9 Michael 10 10 Kevin

How to get Last 5 Row of Data Frame in R

> tail(emp_data,5) emp_id emp_names 16 16 Naresh 17 17 Jagan 18 18 Puppesh 19 19 Head 20 20 Mark

How to get Last 10 Row of Data Frame in R

> tail(emp_data,10) emp_id emp_names 11 11 Smith 12 12 Peter 13 13 Tiger 14 14 Travis 15 15 Siva 16 16 Naresh 17 17 Jagan 18 18 Puppesh 19 19 Head 20 20 Mark

Read more information about R Data Frame visit

Related Posts

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

Comments are closed.

Powered by k2schools