How to Use rbind and cbind on Single Dataframe

0 votes

Sorry for framing the question incorrectly. As a beginner, I'm attempting to learn R on my own.

I have a situation where,

t1_df

id   name   address
1     x       india
2     y       usa

t2_df

id   name   address
3     a      india
4     b       usa

Now i tried to add extra column "msg" using data.frame i.e

t1_df <- data.frame(t1_df,msg)
t2_df <- data.frame(t2_df,msg)

t1_df

  id   name   address   msg
  1     x       india   hi
  2     y       usa     hello

t2_df

id   name   address   msg
3     a      india     go
4     b       usa      bye

when i tried to do rbind it gives error as col names are not matching because both df's having different col names

When i tried to cbind on both df's into single dataframe is t, it included all the columns from both df's i.e

colnames(t)

id   name   address   t1_msg   id   name   address   t2_msg

But i would like to get the dataframe as

id   name   address   t1_msg   t2_msg
  1     x       india   hi       NA
  2     y       usa     hello    NA
  3     a      india     NA      go
  4     b       usa      NA      bye

How do I obtain the output that I described above?

Help me out, please.

Jul 22, 2022 in Data Analytics by avinash
• 1,840 points
741 views

1 answer to this question.

0 votes

To obtain the desired output, you can follow these steps:

  1. Create separate data frames for msg column in t1_df and t2_df. Example:

    t1_msg <- data.frame(msg = c("hi", "hello"))
    t2_msg <- data.frame(msg = c("go", "bye"))
     
  2. Use cbind() to combine the dataframes while maintaining the desired column order. Example:

    t <- cbind(id = c(t1_df$id, t2_df$id),
               name = c(t1_df$name, t2_df$name),
               address = c(t1_df$address, t2_df$address),
               t1_msg = t1_msg$msg,
               t2_msg = t2_msg$msg)
    

    This will create a new dataframe t with the desired column arrangement.

The resulting t dataframe will have the desired structure:

  id   name   address   t1_msg   t2_msg
  1     x       india   hi       NA
  2     y       usa     hello    NA
  3     a      india     NA      go
  4     b       usa      NA      bye
 

Make sure the order of columns and the number of rows in t1_msg and t2_msg match the corresponding data frames (t1_df and t2_df) to avoid any mismatches.

Transform data into actionable insights with our Data Analytics Course – Enroll today!

answered Jun 22, 2023 by anonymous
• 1,180 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 880 views
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
6,270 views
0 votes
4 answers

How to change font size of text and axes on R plots ?

To change the font size of text ...READ MORE

answered Dec 16, 2020 in Data Analytics by Gitika
• 65,910 points
113,055 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
720 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
805 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,496 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
827 views
0 votes
1 answer

how to use the Box-Cox power transformation in R

Yes, you are on the right track ...READ MORE

answered Jun 22, 2023 in Data Analytics by anonymous
• 1,180 points
563 views
0 votes
1 answer

What is the difference between rm() and rm(list=ls())?

Yes, you can clear all variables in ...READ MORE

answered Jun 22, 2023 in Data Analytics by anonymous
• 1,180 points
302 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP