# Get the number of rows and columns in R

Filed Under: R Programming Hello, readers! In this article, we will be focusing on the concept of rows and columns in R i.e. get the number of rows and columns of an object in R programming, in detail.

So, let us begin!! 🙂

Be it a matrix or a data frame, we deal with the data in terms of rows and columns. In the data analysis field, especially for statistical analysis, it is necessary for us to know the details of the object i.e. the count of the rows and columns which represent the data values.

R programming provides us with some easy functions to get the related information at ease! So, let us have a look at it one by one.

## The ncol() function in R programming

R programming helps us with `ncol()` function by which we can get the information on the count of the columns of the object.

That is, ncol() function returns the total number of columns present in the object.

Syntax:

```ncol(object)
```

We need to pass the object that contains the data. Here, the object can be a data frame or even a matrix or a data set.

Example: 01

In the below example, we have created a matrix as shown below. Further, using ncol() function, we try to get the value of the number of columns present in the matrix.

```rm(list = ls())
data = matrix(c(10,20,30,40),2,6)
print(data)
print('Number of columns of the matrix: ')
print(ncol(data))

```

Output:

```> print(data)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]   10   30   10   30   10   30
[2,]   20   40   20   40   20   40
> print('Number of columns of the matrix: ')
 "Number of columns of the matrix: "
> print(ncol(data))
 6
```

Example 02:

Here, we have imported the Bank Loan Defaulter prediction dataset into the R environment using read.csv() function. You can find the dataset here!

Using ncol() function, we detect and extract the count of columns in the dataset.

```rm(list = ls())
getwd()
print('Number of columns: ')
print(ncol(dta))
```

Output:

```Number of columns:
9
```

## The nrow() function in R programming

Having understood about columns, it’s now the time to discuss about the rows for an object.

R provides us `nrow()` function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.

Syntax:

```nrow(object)
```

Example 01:

In this example, we have created a matrix using `matrix()` function in R. Further, we have performed the nrow() function to get the number of rows present in the matrix as shown–

```rm(list = ls())
data = matrix(c(10,20,30,40),2,6)
print(data)
print('Number of rows of the matrix: ')
print(nrow(data))

```

Output:

```> print(data)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]   10   30   10   30   10   30
[2,]   20   40   20   40   20   40

"Number of rows of the matrix: "
 2
```

Example 02:

Now, in this example, we have made use of the same Bank Load Defaulter dataset as mentioned the ncol() function section above!

Having loaded the dataset into the R environment, we make use of `nrow()` function to extract the number of rows present in the dataset.

```rm(list = ls())
getwd()
print('Number of rows: ')
print(nrow(dta))
```

Output:

```"Number of rows: "
850
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned with us.

Till then, Happy Learning!! 🙂

close
Generic selectors
Exact matches only
Search in title
Search in content