Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Rbind & Cbind

It is easy to extract data from the rows and columns of a data frame but there are situations where the data frame needs to be in a format that is different from the format in which it was received.

Data Reshaping in R is about changing the way data is organized into rows and columns.

Joining columns and rows in a data frame is achieved by 2 functions:

rbind (Combines vector, matrix or data frame by rows)

cbind (Combines vector, matrix or data frame by columns)

Transpose

To convert the rows into columns and columns into rows (essentially flipping the data frame) - use the t() function in R.

 

 

 

 

 

 

 

 

 

 

 

The t function is used to create the transpose, after which as.data.frame is used to convert the result into a data frame.

Liaison Librarian

Profile Photo
Martin Morris
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill
Social: Twitter Page

McGill LibraryQuestions? Ask us!
Privacy notice