Rene Chan
Rene Chan

Reputation: 985

Modify part of data frame based on date

I have two dataset, and i would like to modify a section of one column. My first dataset has 4 column, date and variable Type A, B and C.

Main_df
Date    TypeA   TypeB   TypeC
1/1/2019    0   0   0
2/1/2019    0   0   0
3/1/2019    0   0   0
4/1/2019    0   100 0
5/1/2019    0   110 0
6/1/2019    0   120 0
7/1/2019    0   130 0
8/1/2019    0   140 0
9/1/2019    0   150 0
10/1/2019   100 0   0
11/1/2019   120 0   0
12/1/2019   130 0   0

The second has fewer rows and only one column, which in this case is called TypeB, and should be added to the current values of TypeB in the main dataset at the matching dates

df_i
Date    TypeB
2/1/2019    120
3/1/2019    130
4/1/2019    140
5/1/2019    150

The result should look like this :

Main_df
Date    TypeA   TypeB   TypeC
1/1/2019    0   0   0
2/1/2019    0   120 0
3/1/2019    0   130 0
4/1/2019    0   240 0
5/1/2019    0   260 0
6/1/2019    0   120 0
7/1/2019    0   130 0
8/1/2019    0   140 0
9/1/2019    0   150 0
10/1/2019   100 0   0
11/1/2019   120 0   0
12/1/2019   130 0   0

I know how to modify the entire column, but not just a section, especially not when it has to match a date. but i would be surprise if R cannot do that. Can someone help please ?

Upvotes: 0

Views: 48

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

Using dplyr one way would be to bind the two dataframes together which would give NA for non-matching columns, group_by Date and take sum removing NA values.

library(dplyr)

bind_rows(Main_df, df1) %>%
   group_by(Date) %>%
   summarise_at(vars(TypeA:TypeC), sum, na.rm = TRUE)


# A tibble: 12 x 4
#   Date      TypeA TypeB TypeC
#   <chr>     <int> <int> <int>
# 1 1/1/2019      0     0     0
# 2 10/1/2019   100     0     0
# 3 11/1/2019   120     0     0
# 4 12/1/2019   130     0     0
# 5 2/1/2019      0   120     0
# 6 3/1/2019      0   130     0
# 7 4/1/2019      0   240     0
# 8 5/1/2019      0   260     0
# 9 6/1/2019      0   120     0
#10 7/1/2019      0   130     0
#11 8/1/2019      0   140     0
#12 9/1/2019      0   150     0

Upvotes: 2

Related Questions