Table of Contents

    Data Reshaping in R Programming Language: Techniques and Examples

    Data Reshaping in R Programming Language: Techniques and Examples

    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

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

    > # Create vector objects.
    > city  state  zipcode  
    > # Combine above three vectors into one data frame.
    > addresses  
    > # 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  
    > # 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  
    > # 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
    >