Number of Rows & Columns of Data Frame in R (3 Examples)
In this article you’ll learn how to determine the number of rows and columns of a data frame in R programming.
Creation of Example Data
data(iris) # Loading example data frame head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Loading example data frame head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example 1: Apply the ncol() & length() Functions to Get the Number of Columns of a Data Frame
ncol(iris) # Applying ncol function # [1] 5 |
ncol(iris) # Applying ncol function # [1] 5
length(iris) # Applying length function # [1] 5 |
length(iris) # Applying length function # [1] 5
Example 2: Apply the nrow() Function to Get the Number of Rows of a Data Frame
nrow(iris) # Applying nrow function # [1] 150 |
nrow(iris) # Applying nrow function # [1] 150
Example 3: Apply the dim() Function to Get the Dimensions of a Data Frame
dim(iris) # Applying dim function # [1] 150 5 |
dim(iris) # Applying dim function # [1] 150 5