Skip to content

Topic: Data science

  • The head function in R

    The head() function in R returns the first part of a vector, matrix, table, data frame or function that is passed to it as a parameter.

    An example is given below:

    Example of using the head function in R

    The file ‘getdata_data_ss06hid.csv’ is loaded from the current directory. The directory has been set up before (see the post “Reading CSV data with R” for details).

    The View(mydata) function will show the CSV data loaded in R. This will help you see what’s inside the CSV file and what has been loaded.

    The head(mydata) function returns the first six rows of data in table format, including all columns. If the columns do not fit in the display, the R Console will format them over several small tables, as in the example. If you want to get the last six rows,  use the tail() function, which reads data from the end.