Tutorials  /  Python

Rows and Columns in R Programming

Ccentron Redaktion · November 2024 ·4 min read ·Python, Tutorial

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.

VM

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:

Code
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.

Python
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:

Code
> 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] 6

Example 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.

Python
rm(list = ls())
getwd()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of columns: ')
print(ncol(dta))

Output:

Code
Number of columns: 
9

The 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:

Code
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–

Python
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:

Code
> 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] 2

Example 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.

Python
rm(list = ls())
getwd()
#Load the dataset
dta = read.csv("bank-loan.csv",header=TRUE)
print('Number of rows: ')
print(nrow(dta))

Output:

Code
"Number of rows: "
850
Jetzt 200 € Guthaben sichern

Testen 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.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren