Home > Blockchain >  Replace a vector of values with 1 or 0 if they are above 1 or below 0
Replace a vector of values with 1 or 0 if they are above 1 or below 0

Time:11-23

I take 20% above and below the base-case value for each of a set of parameters I have as follows:

`d_e

Minimum_d_e <- d_e - 0.20d_e Maximum_d_e <- d_e 0.20d_e`

Once I have the maximum and minimum values (20% either side of the base-case value) I then create a min parameter values and max parameter values vector as follows:

min = c(Minimum_HR_FP_Exp, Minimum_HR_FP_SoC, Minimum_HR_PD_SoC, Minimum_HR_PD_Exp, Minimum_P_OSD_SoC, Minimum_P_OSD_Exp, Minimum_p_FA1_STD, Minimum_p_FA2_STD, Minimum_p_FA3_STD, Minimum_p_FA1_EXPR, Minimum_p_FA2_EXPR, Minimum_p_FA3_EXPR, Minimum_administration_cost, Minimum_c_PFS_Folfox, Minimum_c_PFS_Bevacizumab, Minimum_c_OS_Folfiri, Minimum_c_AE1, Minimum_c_AE2, Minimum_c_AE3, Minimum_d_e, Minimum_d_c, Minimum_u_F, Minimum_u_P, Minimum_AE1_DisUtil, Minimum_AE2_DisUtil, Minimum_AE3_DisUtil)

max = c(Maximum_HR_FP_Exp, Maximum_HR_FP_SoC, Maximum_HR_PD_SoC, Maximum_HR_PD_Exp, Maximum_P_OSD_SoC, Maximum_P_OSD_Exp, Maximum_p_FA1_STD, Maximum_p_FA2_STD, Maximum_p_FA3_STD, Maximum_p_FA1_EXPR, Maximum_p_FA2_EXPR, Maximum_p_FA3_EXPR, Maximum_administration_cost, Maximum_c_PFS_Folfox, Maximum_c_PFS_Bevacizumab, Maximum_c_OS_Folfiri, Maximum_c_AE1, Maximum_c_AE2, Maximum_c_AE3, Maximum_d_e, Maximum_d_c, Maximum_u_F, Maximum_u_P, Maximum_AE1_DisUtil, Maximum_AE2_DisUtil, Maximum_AE3_DisUtil) The utility values I have above should (Maximum_AE3_DisUtil) not be any greater than 1 (100%) or lower than 0 (0%), I was going to manually replace each one as:

`Maximum_AE3_DisUtil<- replace(Maximum_AE3_DisUtil, Maximum_AE3_DisUtil<0, 0)

Maximum_AE3_DisUtil<- replace(Maximum_AE3_DisUtil, Maximum_AE3_DisUtil>1, 1)`

I tried the above manual approach, which does work, but is probably less efficient than it could be.

CodePudding user response:

Here is a way using a simple compound test:

x <- sample(seq(-0.1, 1.1, .1), 100, replace = TRUE)
x
  [1]  0.3  1.1  0.5  0.2  0.7  0.9  0.7  0.5  0.0  0.4  1.0  0.5  0.9  1.0  1.1  0.1  1.0  0.1 -0.1  1.1  0.3  1.1
 [23]  0.1  0.6  0.0  0.8  0.6 -0.1  1.0  0.3  0.1  0.2  0.1  0.5  0.1  1.0  0.8  0.1  0.3  0.8  0.5  0.5  0.3  0.2
 [45]  0.6  0.0  1.1  0.0  0.6  0.8  0.3  0.0  0.0  0.2  0.6  0.2  1.0  0.0  0.4  0.2  0.8  0.2  0.4  0.2  1.1  0.5
 [67]  0.8  0.1  1.0 -0.1  0.5  0.4  0.6  0.6  0.8  0.0  0.4  0.3  0.3  0.0  0.3  0.3  0.0  1.0 -0.1  1.1  1.1 -0.1
 [89]  0.9  0.9  0.3  0.9  0.7  0.5  0.1  0.4  1.1  0.9  1.0  0.8
x <- as.numeric(x > 0 & x < 1)
x
  [1] 1 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 1 1
 [56] 1 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 1

Replace x with your vector.

CodePudding user response:

I'm not really sur what you're working with, but assuming it's a data.frame, a tibble or a data.table, if you want to make it super efficient, I'd simply cast it as a data.table and then:

dt[, Maximum_AE3_DisUtil := ifelse(
    Maximum_AE3_DisUtil < 0, 0, ifelse(
        Maximum_AE3_DisUtil > 1, 1
    )
)]

If you aren't familiar with data.table this is a change of value by reference and the datatable "dt" will be altered in place, meaning you don't have to do dt <- dt[...]

  •  Tags:  
  • r
  • Related