R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
The nrow()
function in R returns the number of rows present in an object, such as a matrix, data frame, or array. When working with data, it's often essential to know how many rows (or observations) you're dealing with, especially during the initial stages of data exploration.
In this tutorial, we will examine the use of the nrow()
function across various R objects.
For matrices, the number of rows can be obtained directly using the nrow()
function.
mat <- matrix(1:6, nrow = 2) # Get the number of rows row_count <- nrow(mat) print(row_count) # Outputs: 2
nrow()
with Data FramesFor data frames, nrow()
gives the number of rows or observations present.
df <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6)) # Get the number of rows row_count <- nrow(df) print(row_count) # Outputs: 3
nrow()
with ArraysWith a 3-dimensional array, nrow()
will consider the first dimension and return the row count.
array_data <- array(1:12, dim = c(2, 3, 2)) # Get the number of rows in the first dimension row_count <- nrow(array_data) print(row_count) # Outputs: 2
It's worth noting that when you use nrow()
on a simple numeric or character vector, the result will be NULL
, as vectors don't inherently have the concept of rows.
vec <- c(1, 2, 3, 4) # Get the number of rows row_count <- nrow(vec) print(row_count) # Outputs: NULL
If you're looking for more details about an object's structure, the dim()
function is useful. It returns both the number of rows and columns in a vector format.
df <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6)) # Get the dimensions (rows and columns) dimensions <- dim(df) print(dimensions) # Outputs: 3 2 # The number of rows can be extracted as row_count <- dimensions[1] print(row_count) # Outputs: 3
The nrow()
function is a simple yet indispensable tool when analyzing datasets in R. Having a clear understanding of the number of rows or observations you're working with can greatly influence subsequent analytical decisions, and nrow()
provides this information effortlessly.
nrow() function in R:
nrow()
function in R is used to find the number of rows in a matrix or data frame. It returns an integer representing the number of rows.# Using nrow() in R my_matrix <- matrix(1:6, nrow = 2, ncol = 3) num_rows <- nrow(my_matrix)
Finding the number of rows in R:
nrow()
function to find the number of rows in a matrix in R.# Finding the number of rows in R my_matrix <- matrix(1:6, nrow = 2, ncol = 3) num_rows <- nrow(my_matrix)
Getting the number of rows in a matrix in R:
nrow()
function to obtain the number of rows in a matrix in R.# Getting the number of rows in a matrix in R my_matrix <- matrix(1:6, nrow = 2, ncol = 3) num_rows <- nrow(my_matrix)
Using nrow() for row statistics in R:
nrow()
function for obtaining basic statistics on the rows of a matrix in R.# Using nrow() for row statistics in R my_matrix <- matrix(1:6, nrow = 2, ncol = 3) row_stats <- nrow(my_matrix)
Finding the number of rows in R data frames:
nrow()
function.# Finding the number of rows in R data frames my_data <- data.frame(A = c(1, 2), B = c(3, 4)) num_rows <- nrow(my_data)
Row count in R matrices:
nrow()
function to count the number of rows in R matrices.# Row count in R matrices my_matrix <- matrix(1:6, nrow = 2, ncol = 3) num_rows <- nrow(my_matrix)
Conditional row count with nrow() in R:
nrow()
function with conditions to count rows satisfying a specific criterion.# Conditional row count with nrow() in R my_matrix <- matrix(1:6, nrow = 2, ncol = 3) condition <- rowSums(my_matrix > 3) > 1 conditional_row_count <- sum(condition)