R Calculating relative differences between values that contain 0

75 Views Asked by At

I have a dataframe dat, which shows the number of daily visitors during certain days (case) and other days (control). I want to create now a 4th column, in which the difference between the Case and Control columns is being calculated for each Var. The problem is, that I dont want differences like +0.30 in that first case. I actually want something like +400%. But I have a lot of 0 in the Control column, so I always get Inf. numbers, which I dont want to have. How can I solve that problem?

  Var  Case Control
1  AB  0.40    0.10
2  TG  3.20    6.30
3  GF  0.23    0.18
4  ZH 14.00   14.00
5  ED  7.10    0.00
6  MS  5.50    5.60

dat <- data.frame(Var = c("AB", "TG", "GF", "ZH", "ED", "MS"),
                  Case = c(0.4, 3.2, 0.23, 14, 7.1, 5.5),
                  Control = c(0.1, 6.3, 0.18, 14, 0, 5.6))
0

There are 0 best solutions below