dim, nrow & ncol Functions in R
The following R code explains how to inspect the dimensions of a data frame with the dim(), nrow(), and ncol() functions of the R programming language.
Example Data Frame
my_data <- data.frame(x = c(3, 1, 6, 3, 2), # Create example data y = "a", z = c(2, 2, 1, 1, 2)) my_data # Print example data # x y z # 3 a 2 # 1 a 2 # 6 a 1 # 3 a 1 # 2 a 2 |
my_data <- data.frame(x = c(3, 1, 6, 3, 2), # Create example data y = "a", z = c(2, 2, 1, 1, 2)) my_data # Print example data # x y z # 3 a 2 # 1 a 2 # 6 a 1 # 3 a 1 # 2 a 2
Example: Application of dim, nrow & ncol Functions in R
The dimensions (i.e. the number of columns AND rows) are 5 and 3:
dim(my_data) # Apply dim function # 5 3 |
dim(my_data) # Apply dim function # 5 3
The number of rows is 5:
nrow(my_data) # Apply nrow function # 5 |
nrow(my_data) # Apply nrow function # 5
The number of columns is 3:
ncol(my_data) # Apply ncol function # 3 |
ncol(my_data) # Apply ncol function # 3