Joining Rows and Columns in Data Frame

In R, we can join two vectors or merge two data frames using functions. There are basically two functions that perform these tasks:

cbind():

We can combine vectors, matrix or data frames by columns using cbind() function.

Syntax: cbind(x1, x2, x3)

where x1, x2 and x3 can be vectors or matrices or data frames. 

rbind():

We can combine vectors, matrix or data frames by rows using rbind() function.

Syntax: rbind(x1, x2, x3)

where x1, x2 and x3 can be vectors or matrices or data frames.

Example:

R




# Cbind and Rbind function in R
name <- c("Shaoni", "esha", "soumitra", "soumi")
age <- c(24, 53, 62, 29)
address <- c("puducherry", "kolkata", "delhi", "bangalore")
 
# Cbind function
info <- cbind(name, age, address)
print("Combining vectors into data frame using cbind ")
print(info)
 
# creating new data frame
newd <- data.frame(name=c("sounak", "bhabani"),
                   age=c("28", "87"),
                   address=c("bangalore", "kolkata"))
 
# Rbind function
new.info <- rbind(info, newd)
print("Combining data frames using rbind ")
print(new.info)


Output:

[1] "Combining vectors into data frame using cbind "
name age address
[1,] "Shaoni" "24" "puducherry"
[2,] "esha" "53" "kolkata"
[3,] "soumitra" "62" "delhi"
[4,] "soumi" "29" "bangalore"

[1] "Combining data frames using rbind "

name age address
1 Shaoni 24 puducherry
2 esha 53 kolkata
3 soumitra 62 delhi
4 soumi 29 bangalore
5 sounak 28 bangalore
6 bhabani 87 kolkata

Data Reshaping in R Programming

Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame from the one we receive. Hence, in R, we can split, merge and reshape the data frame using various functions.

The various forms of reshaping data in a data frame are:

  • Transpose of a Matrix
  • Joining Rows and Columns
  • Merging of Data Frames
  • Melting and Casting

Similar Reads

Why R – Data Reshaping is Important?

While doing an analysis or using an analytic function, the resultant data obtained because of the experiment or study is generally different. The obtained data usually has one or more columns that correspond or identify a row followed by a number of columns that represent the measured values. We can say that these columns that identify a row can be the composite key of a column in a database....

Transpose of a Matrix

We can easily calculate the transpose of a matrix in R language with the help of the t() function. The t() function takes a matrix or data frame as an input and gives the transpose of that matrix or data frame as its output....

Joining Rows and Columns in Data Frame

...

Merging two Data Frames

In R, we can join two vectors or merge two data frames using functions. There are basically two functions that perform these tasks:...

Melting and Casting

...

Contact Us