R programming Merging in R Programming

0 votes

I'm trying to merge 2 data frames where each data frame has the same column name 'Date_Time' but different row counts. These are my data frames:

df1

Cmny_Name     Date_Time      Price 
A          1/1/2015 13:27    1083
B          1/1/2015 13:28    1084
C          1/1/2015 13:29    1053
D          1/1/2015 13:31    1063
E          1/1/2015 13:33    1033

df2:

Cmny_Name1     Date_Time      Price 
A          1/1/2015 13:27    1043
A          1/1/2015 13:28    1053
A          1/1/2015 13:29    1054
A          1/1/2015 13:35    1084

I want my output to look like this: (df3)

Date_Time      Cmny_Name    Price   Cmny_Name1     Price   
1/1/2015 13:27   A          1083        A          1043
1/1/2015 13:28   B          1084        A          1053
1/1/2015 13:29   C          1053        A          1054
1/1/2015 13:31   D          1063        A           NA
1/1/2015 13:33   E          1033        A           NA
1/1/2015 13:35   NA         NA          A          1084
Apr 24, 2019 in Data Analytics by Tyrion anex
• 8,700 points
780 views

1 answer to this question.

0 votes

You can merge the two data frames by following the below code snippet:

df3 <- merge(df1, df2, by = "Date_Time", all = TRUE)

Another way is to use the dplyr package:

library(dplyr)
df3 <- df1 %>% 
  full_join(df2, by = "Date_Time")


Hope it helps!

To learn more about R, enroll with data science with R programming course today.

Thanks.

answered Apr 24, 2019 by Sophie may
• 10,620 points

Related Questions In Data Analytics

0 votes
2 answers

What are the rules to define a variable name in R programming language?

The same rules almost follow for all ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
15,111 views
+1 vote
1 answer

Difference between factor and as.factor in R programming

Hey @Ali, as.factor is a wrapper for ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,840 points
4,858 views
0 votes
1 answer

What is active binding in R programming

Active bindings in R are much like ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
1,462 views
+1 vote
1 answer

Remove NA values from the output in R programming

Edit your code: columnmean <- function(x, removeNA = ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
875 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,613 views
+1 vote
1 answer

Error saying "vector size cannot be NA" when using R with data mining

You can use the removesparseterm function.  Removes sparse ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
4,815 views
+1 vote
2 answers
0 votes
1 answer

Trying to find frequent itemsets of a data set using arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", "milk", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
799 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 10,620 points
683 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 28, 2018 in Data Analytics by Sophie may
• 10,620 points
716 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