View First Rows of Data in R with head()

In R language, you can use the head() function to view the first few rows of data. The syntax of the head() function is as follows:

head(data, n)

In this case, data refers to the object of data being viewed, which can be a data frame, matrix, or vector; n represents the number of rows to be displayed, defaulting to 6.

Here is an example:

# 创建一个数据框
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "David", "Eve"),
  Age = c(25, 30, 35, 40, 45),
  Salary = c(5000, 6000, 7000, 8000, 9000)
)

# 查看数据框的前3行
head(data, 3)

The output result is:

     Name Age Salary
1   Alice  25   5000
2     Bob  30   6000
3 Charlie  35   7000

The head() function displays the first few rows of a data object. By default, it shows the first 6 rows.

bannerAds