r - Merging two dataframes with different size -


i have 2 data frames 2 columns. 1 column date numeric data. 2 data frames have different size. give example of have , need.

this have:

df1 2015-01-02  0 2015-01-03  0 2015-01-04  0  df2 2015-01-03  200 

this need:

df1 2015-01-02  0 2015-01-03  200 2015-01-04  0 

i have tried comparing (compare function) both df have no solution. maybe (or make functions faster), in both df dates sorted.

could me?

thank much, gobya

it's not clear how want choose row select when there matching dates 2 data frames (per @user295691's comment), i've provided 2 selection options below give result specified.

df1 <- data.frame(date = c("2015-01-02", "2015-01-03", "2015-01-04"),                   value = c(0, 0, 0), stringsasfactors=false) df2 <- data.frame(date = c("2015-01-03"), value = c(200), stringsasfactors=false)  df1$source = "df1" df2$source = "df2"  library(dplyr)  # choose greatest value each date newdf = df1 %>% bind_rows(df2) %>%   group_by(date) %>%   filter(value == max(value))  # if there more 2 values given date,  # choose value(s) df2 date newdf = df1 %>% bind_rows(df2) %>%   group_by(date) %>%   mutate(n=n()) %>%   filter(ifelse(n>1, source=="df2", source=="df1")) %>%   select(-n) 

fyi, second approach, thought following work, excludes rows date=2014-01-03. i'm not sure why , interested in ideas on what's going wrong:

df1 %>% bind_rows(df2) %>%   group_by(date) %>%   filter(ifelse(n() > 1, source=="df2", source=="df1"))          date value source 1 2015-01-02     0    df1 2 2015-01-04     0    df1 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -