head

Get top rows of table, timetable, or tall array

Description

example

B = head(A) returns the first eight rows of table or timetable A.

example

B = head(A,k) returns the first k rows of A.

Examples

collapse all

Create a table that contains 100 rows and five variables.

load patients
T = table(LastName,Gender,Age,Height,Weight);
size(T)
ans = 1×2

   100     5

Preview the first eight rows.

T2 = head(T)
T2=8×5 table
      LastName        Gender      Age    Height    Weight
    ____________    __________    ___    ______    ______

    {'Smith'   }    {'Male'  }    38       71       176  
    {'Johnson' }    {'Male'  }    43       69       163  
    {'Williams'}    {'Female'}    38       64       131  
    {'Jones'   }    {'Female'}    40       67       133  
    {'Brown'   }    {'Female'}    49       64       119  
    {'Davis'   }    {'Female'}    46       68       142  
    {'Miller'  }    {'Female'}    33       64       142  
    {'Wilson'  }    {'Male'  }    40       68       180  

Create a tall table and preview the first few rows of data.

Create a tall table for the airlinesmall.csv data set. Select a subset of the variables to work with. Use head to extract the first few rows of data.

varnames = {'Year','Month','ArrDelay','DepDelay','UniqueCarrier'};
ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA',...
    'SelectedVariableNames',varnames);
T = tall(ds)
T =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    
     :        :         :           :              :
     :        :         :           :              :
tt = head(T)
tt =

  8x5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    

Collect the results into memory to view the data.

t8 = gather(tt)
t8=8×5 table
    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    

Preview the first 20 rows of data in a tall table.

Create a tall table for the airlinesmall.csv data set. Select a subset of the variables to work with, and treat 'NA' values as missing data so that datastore replaces them with NaN values. Use head to view the first 20 rows of data.

varnames = {'Year','Month','ArrDelay','DepDelay','UniqueCarrier'};
ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA',...
    'SelectedVariableNames',varnames);
T = tall(ds)
T =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    
     :        :         :           :              :
     :        :         :           :              :
tt = head(T,20)
tt =

  20x5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    
     :        :         :           :              :
     :        :         :           :              :

Collect the results into memory to view the data.

t20 = gather(tt)
t20=20×5 table
    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12          {'PS'}    
    1987     10          8           1          {'PS'}    
    1987     10         21          20          {'PS'}    
    1987     10         13          12          {'PS'}    
    1987     10          4          -1          {'PS'}    
    1987     10         59          63          {'PS'}    
    1987     10          3          -2          {'PS'}    
    1987     10         11          -1          {'PS'}    
    1987     10          3           3          {'PS'}    
    1987     10          2           1          {'PS'}    
    1987     10         16          15          {'PS'}    
    1987     10          3           9          {'PS'}    
    1987     10         39          15          {'PS'}    
    1987     10         57          32          {'TW'}    
    1987     10          0          -3          {'TW'}    
    1987     10        -14           0          {'TW'}    
      ⋮

Input Arguments

collapse all

Input array, specified as a table or timetable.

Data Types: table | timetable

Number of rows to extract, specified as a positive scalar integer. If A has fewer than k rows, then head returns all of A.

Output Arguments

collapse all

Requested rows, returned as a table or timetable. The data type of B is the same as A.

Extended Capabilities

Introduced in R2016b