Suppose I have the following data.frame:
dt = tibble::tibble(
id_0 = rep(123, 6),
name_0 = rep("A", 6),
id_1 = c(rep(321, 3), rep(322, 3)),
name_1 = c(rep("B", 3), rep("C", 3)),
p_1 = c(rep(0.7, 3), rep(0.3, 3)),
id_2 = c(NA, 323:326, NA),
name_2 = c(NA, "D", "E", "J", "G", NA),
p_2 = c(NA, 0.8, 0.2, 0.9, 0.1, NA),
id_3 = c(NA, NA, 323, NA, NA, NA),
na_3 = c(NA, NA, "H", NA, NA, NA),
p_3 = c(NA, NA, 1, NA, NA, NA),
)
Which looks like this:
# A tibble: 6 x 11
id_0 name_0 id_1 name_1 p_1 id_2 name_2 p_2 id_3 na_3
<dbl> <chr> <dbl> <chr> <dbl> <int> <chr> <dbl> <dbl> <chr>
1 123 A 321 B 0.7 NA NA NA NA NA
2 123 A 321 B 0.7 323 D 0.8 NA NA
3 123 A 321 B 0.7 324 E 0.2 323 H
4 123 A 322 C 0.3 325 J 0.9 NA NA
5 123 A 322 C 0.3 326 G 0.1 NA NA
6 123 A 322 C 0.3 NA NA NA NA NA
I need to take the row-by-row product from al the p_* columns. In this case it would be Product = p_1 * p_2 * p_3, but in general it could be any product from p_1 to p_* (this data.frame changes from case to case, I mean Product = product(p_1, p_2, ..., p_n)). Note that p_* is always greater than cero and less or equal than one (p_ > 0 & p_ <= 1). So the task I need to accomplish have two things: The Product1) must omit the NAs and 2) be general for any number of p_* present in the data.frame.
The desireble output should look like this:
# A tibble: 6 x 12
id_0 name_0 id_1 name_1 p_1 id_2 name_2 p_2 id_3 na_3 p_3 Product
<dbl> <chr> <dbl> <chr> <dbl> <int> <chr> <dbl> <dbl> <chr> <dbl> <dbl>
1 123 A 321 B 0.7 NA NA NA NA NA NA 0.7
2 123 A 321 B 0.7 323 D 0.8 NA NA NA 0.56
3 123 A 321 B 0.7 324 E 0.2 323 H 1 0.14
4 123 A 322 C 0.3 325 J 0.9 NA NA NA 0.27
5 123 A 322 C 0.3 326 G 0.1 NA NA NA 0.03
6 123 A 322 C 0.3 NA NA NA NA NA NA 0.3