In this article, we will be focusing on the concept of rows and columns in R programming i.e. get the number of rows and columns of an object in R programming, in detail.
Introduction
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.
Matching infrastructure at centron
Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →
The ncol() Function in R
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 - ncol() Function in R-Programming
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: ')
[1] "Number of columns of the matrix: "
> print(ncol(data))
[1] 6Example 02 - ncol() Function in R-Programming
Here, we have imported the Bank Loan Defaulter prediction dataset into the R environment using read.csv() function.
Using ncol() function, we detect and extract the count of columns in the dataset.
rm(list = ls())
getwd()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of columns: ')
print(ncol(dta))Output:
Number of columns:
9The nrow() Function in R
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 - nrow() Function in R-Programming
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: "
[1] 2Example 02 - nrow() Function in R-Programming
Now, in this example, we have made use of the same Bank Loan Defaulter dataset as mentioned in 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()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of rows: ')
print(nrow(dta))Output:
"Number of rows: "
850Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.