I have a dataset, input_ds_wt that contains values for RSE_VAR and wt.mean_VAR for many variables, VAR.
input_ds_wt = structure(list(id = c(1, 2, 3, 4, 5, 6), wt.mean_v1 = c(1, 1,
1.3, 2.3, 1, 0), wt.mean_v2 = c(0.8, 0.2, 0.8, 0.2, 0.8, 0.2),
wt.SE_v1 = c(0.1, 0.01, 0.2, 0.02, 0.3, 0.03), wt.SE_v2 = c(0.03,
0.3, 0.01, 0.1, 0.4, 0.04), RSE_v1 = c(0.1, 0.01, 0.153846153846154,
0.00869565217391304, 0.3, Inf), RSE_v2 = c(0.0375, 1.5, 0.0125,
0.5, 0.5, 0.2)), class = "data.frame", row.names = c(NA,
-6L))
gives
input_ds_wt
id wt.mean_v1 wt.mean_v2 wt.SE_v1 wt.SE_v2 RSE_v1 RSE_v2
1 1 1.0 0.8 0.10 0.03 0.100000000 0.0375
2 2 1.0 0.2 0.01 0.30 0.010000000 1.5000
3 3 1.3 0.8 0.20 0.01 0.153846154 0.0125
4 4 2.3 0.2 0.02 0.10 0.008695652 0.5000
5 5 1.0 0.8 0.30 0.40 0.300000000 0.5000
6 6 0.0 0.2 0.03 0.04 Inf 0.2000
For every VAR I want the user-defined function suppress_fn() to return a 0 if RSE_VAR < 0.3 and wt.mean_VAR > 0.9, and 1 else. Therefore I want it to return:
output_supress
id wt.mean_v1 wt.mean_v2 wt.SE_v1 wt.SE_v2 RSE_v1 RSE_v2 suppress_v1 suppress_v2
1 1 1.0 0.8 0.10 0.03 0.100000000 0.0375 0 1
2 2 1.0 0.2 0.01 0.30 0.010000000 1.5000 0 1
3 3 1.3 0.8 0.20 0.01 0.153846154 0.0125 0 1
4 4 2.3 0.2 0.02 0.10 0.008695652 0.5000 0 1
5 5 1.0 0.8 0.30 0.40 0.300000000 0.5000 1 1
6 6 0.0 0.2 0.03 0.04 Inf 0.2000 1 1`
which I have created using the following statement, but I want to do it for all variables in VAR without specifying same suppress_VAR function the same number of times as the dimension of VAR. Can anyone show me how to do it using dplyr:: pivot_longer followed by pivot_wider? Other methods welcome too.
output_supress = input_ds_wt %>%
mutate(suppress_v1 = if_else(RSE_v1 < 0.3 & wt.mean_v1 > 0.9,0, 1 ),
suppress_v2 = if_else(RSE_v2 < 0.3 & wt.mean_v2 > 0.9,0, 1 ) )
output_supress
id wt.mean_v1 wt.mean_v2 wt.SE_v1 wt.SE_v2 RSE_v1 RSE_v2 suppress_v1 suppress_v2
1 1 1.0 0.8 0.10 0.03 0.100000000 0.0375 0 1
2 2 1.0 0.2 0.01 0.30 0.010000000 1.5000 0 1
3 3 1.3 0.8 0.20 0.01 0.153846154 0.0125 0 1
4 4 2.3 0.2 0.02 0.10 0.008695652 0.5000 0 1
5 5 1.0 0.8 0.30 0.40 0.300000000 0.5000 1 1
6 6 0.0 0.2 0.03 0.04 Inf 0.2000 1 1