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?
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.
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.
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.
If you want to print the last 10 lines, use
tail(dataset, 10)
for the first 10, you could also do
head(dataset, 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