0

I have a data.frame that I want to manipulate in a way that the two rows containing USD (and EUR) in CP variable get joined:

Datum CP mnozstvi close
2021-07-30 USD 10 NA
2021-07-30 EUR 5 NA
2021-07-30 X 1 1
2021-07-30 X 1 1
2021-07-30 X 1 1
2021-07-30 USD NA 10
2021-07-30 EUR NA 5

Desired data.frame:

Datum CP mnozstvi close
2021-07-30 USD 10 10
2021-07-30 EUR 5 5
2021-07-30 X 1 1
2021-07-30 X 1 1
2021-07-30 X 1 1

I dont know how to do it. Ideally using dplyr. this is just minimal case, mine is mooore complicated.

JEŠTĚR
  • 53
  • 4
  • 1
    Try `coalesce()` https://stackoverflow.com/questions/55642210/how-to-merge-2-columns-within-the-same-dataframe-in-r/55642536#55642536 & https://stackoverflow.com/questions/14268814/merge-rows-in-a-dataframe-where-the-rows-are-disjoint-and-contain-nas/52524260#52524260 – Tung Aug 04 '21 at 22:12
  • Does not work in this case... I want to basically combine two rows containing USD ( and EUR) in CP variable – JEŠTĚR Aug 04 '21 at 22:24
  • 2
    If things are always even like this (same # NA in each group), maybe remove NA with something like `dat %>% group_by(Datum, CP) %>% summarise(across(mnozstvi:close, na.omit))`. – aosmith Aug 04 '21 at 22:57

0 Answers0