-3

I have the following set of data:

enter image description here

The rows in Yellow is an example of a good situation, because for Vaer=B both Var2=F and Var2=G a freq value is present.

But, rows in red are example of a bad situation, because row 13 where Var2=F have freq value at date 07.02.2018 but I do not have a value for Var2=G at the same date.

On the other hand, row 27 in red, I have freq value for Var2=G at date 04:02:2018 but I do not have freq value for Var2=F at the same date.

What I need is: For all type of Var1 (A, B, ..) and For each Var2 (F, G) If freq is present in Var2=F but not in Var2=G create for Var2=G freq=0 at the same date.

The same have to be mabe for Var2=G where Var2=F is not present.

Could you give me some idea how to do that in R?

user1010441
  • 59
  • 1
  • 7
  • 4
    Please don't provide data as an image!! Read [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – kath Apr 27 '18 at 09:37

1 Answers1

0

Your explanation is a bit cryptic to me, but I guess you have counts for all groups but you need to have them as well for all permutations, even if there are no observations (therefore fill freq with 0). You can use tidyr::complete:

df <- data.frame(Var1 = c("A", "A", "B", "B", "B"),
                 Var2 = c("F", "G", "F", "G", "F"),
                 freq = c(2L, 3L, 1L, 5L, 3L),
                 date = as.Date(c("2018-04-27", "2018-04-28", "2018-04-27", "2018-04-28", "2018-04-29")))
tidyr::complete(data = df, Var1, Var2, date, fill = list(freq = 0L))
Tino
  • 2,091
  • 13
  • 15