Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Language: How to print the first or last rows of a data set? [duplicate]

Tags:

r

I can print first n rows by the following command:

> dataset[1:n, ] 

Is there a more elegant way to do this? Also, how can I print the last rows of a data set?

like image 928
dev Avatar asked Jan 18 '15 07:01

dev


People also ask

How do you duplicate specific rows in R?

The way to repeat rows in R is by using the REP() function. The REP() function is a generic function that replicates the value of x one or more times and it has two mandatory arguments: x: A vector.

How do I pull the last two rows in R?

The last n rows of the data frame can be accessed by using the in-built tail() method in R. Supposedly, N is the total number of rows in the data frame, then n <=N last rows can be extracted from the structure.

Which function we use to view first six rows in R?

Example 1: Select First 6 Rows with head Function As you can see based on the output of the RStudio console, the head function returned exactly six rows.


1 Answers

If you want to print the last 10 lines, use

tail(dataset, 10) 

for the first 10, you could also do

head(dataset, 10) 
like image 90
MrFlick Avatar answered Sep 20 '22 10:09

MrFlick