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 23 in Data Analytics by Tyrion anex
• 8,310 points
39 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")
answered Apr 23 by Sophie may
• 9,870 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 in Data Analytics by anonymous
• 32,260 points
122 views
0 votes
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,040 points
578 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,040 points
43 views
0 votes
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
• 46,110 points
52 views
0 votes
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,040 points
591 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,040 points
46 views
0 votes
1 answer

Error saying "Error in df$item : object of type 'closure' is not subsettable" when trying to use arules package

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

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,040 points
207 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
• 9,870 points
53 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 27, 2018 in Data Analytics by Sophie may
• 9,870 points
58 views