View the Top Rows in R DataFrame using Head

The head() function can be used to view the top rows in R DataFrame:

head(df, number of top rows needed)

For example, to view the top 3 rows:

# Create a sample DataFrame
df <- data.frame(product = c("computer", "monitor", "keyboard", "printer", "tablet"),
                 price = c(1200, 400, 100, 200, 300),
                 brand = c("A", "B", "C", "X", "Y")
                 )

# View the first three rows using head()
print(head(df, 3))

The result:

   product  price  brand
1 computer   1200     A
2  monitor    400     B
3 keyboard    100     C

You may check the following guide to view the bottom rows in R DataFrame.

Leave a Comment