Explain with an example how to view the first few rows or the last few rows of a dataset in R.

753    Asked by Natunyadav in Data Science , Asked on Nov 19, 2019
Answered by Natun yadav

To do this we have to select a dataframe for selecting rows and columns. A data frame is a structure having 2 dimensions which contains data in the form of rows and columns. It can be spreadsheet data and can be represented in the form of tables or dictionaries of series.

In R, let us read a built in dataset which is iris dataset.It contains the following attributes

Sepal Length

Sepal width

Petal length

Petal width

Based on the following attributes are considered which are the Species.

df=iris

df


Now it contains about 150 rows.To view only the first 5 rows we can choose

head(df,5)


So the above 5 rows contain the species setosa.Let us see the below 5 rows and look into the outcome

To view the last 5 rows we can choose

tail(df,5)


The above rows represent the last rows which belong to the species virginica.


Your Answer

Interviews

Parent Categories