resize data frame

1

How could two dataframes be "resized" or cut by the value of one of their columns? My objective is to operate with them, that is to make the difference of the values for the coinciding dates.

I do not know if it could be done in an efficient way without having to create variables that store the two dataframes and calculate the error.

The Data: two dataframes of 2 columns (time and value) with different lengths, one starts later than the other and ends after the second.

df1
         time   value
    1  2001-02-01 107
    2  2001-04-01 122
    3  2001-06-01 123
    4  2001-08-01 101
    5  2001-10-01 130
    6  2001-12-01 116
    7  2002-02-01 108
    8  2002-04-01 154
    9  2002-06-01 146
    10 2002-08-01 111
    11 2002-10-01 110
    12 2002-12-01 133

df2

  time   value

3  2001-06-01 223
4  2001-08-01 131
5  2001-10-01 134
6  2001-12-01 16
7  2002-02-01 228

dfR Desired result:

3  2001-06-01 100
4  2001-08-01 30
5  2001-10-01 4
6  2001-12-01 -100
7  2002-02-01 120

Edit: # a possible solution is to make a merge for "time" and then operate ...

dfR<- merge(df1,df2, by = c("time"))

Any other better solution?

    
asked by PeCaDe 05.09.2016 в 11:38
source

1 answer

1

If I understand correctly, you want to get dfR as you put it in the example. The easiest thing here is to use dplyr . In this case, it could be:

dfR <- df1 %>% 
  right_join(df2, by="time") %>% 
  transmute(time=time, diferencia=value.y-value.x)

where dfR gives the desired result:

# A tibble: 5 × 2
        time diferencia
      <date>      <int>
1 2001-06-01        100
2 2001-08-01         30
3 2001-10-01          4
4 2001-12-01       -100
5 2002-02-01        120

Look here we did a right_join() , there are other ways to join the tables (leaving or deleting rows) described in the the Domar Datos reference sheet . There also once you can choose to leave the previous results with mutate or ignore it with transmute as we did here above.

    
answered by 04.10.2016 / 21:20
source