Data Reshaping in R Programming Language

Rumman Ansari   Software Engineer   2023-03-24   6138 Share
☰ Table of Contents

Table of Content:


Data Reshaping in R is about changing the way data is organized into rows and columns. Most of the time data processing in R is done by taking the input data as a data frame. It is easy to extract data from the rows and columns of a data frame but there are situations when we need the data frame in a format that is different from format in which we received it. R has many functions to split, merge and change the rows to columns and vice-versa in a data frame.

Joining Columns and Rows in a Data Frame

We can join multiple vectors to create a data frame using the cbind()function. Also we can merge two data frames using rbind() function.

# Create vector objects.
city <- c("Burdwan","Bankura","Darjeeling","Kolkata")
state <- c("WB","WB","WB","WB")
zipcode <- c(713101,722101,734101,700001)

# Combine above three vectors into one data frame.
addresses <- cbind(city,state,zipcode)

# Print a header.
cat("# # # # The First data frame\n") 

# Print the data frame.
print(addresses)

# Create another data frame with similar columns
new.address <- data.frame(
  city = c("Nadia","Malda"),
  state = c("WB","WB"),
  zipcode = c("712147","732101"),
  stringsAsFactors = FALSE
)

# Print a header.
cat("# # # The Second data frame\n") 

# Print the data frame.
print(new.address)

# Combine rows form both the data frames.
all.addresses <- rbind(addresses,new.address)

# Print a header.
cat("# # # The combined data frame\n") 

# Print the result.
print(all.addresses)

When we execute the above code, it produces the following result

> # Create vector objects.
> city <- c("Burdwan","Bankura","Darjeeling","Kolkata")
> state <- c("WB","WB","WB","WB")
> zipcode <- c(713101,722101,734101,700001)
> 
> # Combine above three vectors into one data frame.
> addresses <- cbind(city,state,zipcode)
> 
> # Print a header.
> cat("# # # # The First data frame\n") 
# # # # The First data frame
> 
> # Print the data frame.
> print(addresses)
     city         state zipcode 
[1,] "Burdwan"    "WB"  "713101"
[2,] "Bankura"    "WB"  "722101"
[3,] "Darjeeling" "WB"  "734101"
[4,] "Kolkata"    "WB"  "700001"
> 
> # Create another data frame with similar columns
> new.address <- data.frame(
+   city = c("Nadia","Malda"),
+   state = c("WB","WB"),
+   zipcode = c("712147","732101"),
+   stringsAsFactors = FALSE
+ )
> 
> # Print a header.
> cat("# # # The Second data frame\n") 
# # # The Second data frame
> 
> # Print the data frame.
> print(new.address)
   city state zipcode
1 Nadia    WB  712147
2 Malda    WB  732101
> 
> # Combine rows form both the data frames.
> all.addresses <- rbind(addresses,new.address)
> 
> # Print a header.
> cat("# # # The combined data frame\n") 
# # # The combined data frame
> 
> # Print the result.
> print(all.addresses)
        city state zipcode
1    Burdwan    WB  713101
2    Bankura    WB  722101
3 Darjeeling    WB  734101
4    Kolkata    WB  700001
5      Nadia    WB  712147
6      Malda    WB  732101
>