Imputation help? impute with a minimum value divided by 2

Good day. I am trying to impute NA values in my dataset. I have 600 peptide variables. Each peptide variable has NA values. The following syntax was able to impute the NA values with the minimum of each peptide variables data, but I need to impute the NA value with the minimum divided by 2 (min/2) (therefore, not only the minimum).

Syntax that I wrote:

group.imp <- group1 %>% impute_at(.na=na.min, .vars=379:975)

If I tried to divide the min by 2 (see below), but then it gives an error of: "non-numeric argument to binary operator":

group.imp <- group1 %>% impute_at(.na=na.min/2, .vars=379:975)

Is there anyway that I can impute the NA values of each variable with the minimum of each peptide variable divided by 2?

# load required libraries and make an example called 'tbl'
library(tidyimpute)
library(na.tools)
tbl <- data.frame( col_1 = letters[1:3], col_2=c(1,NA_real_,3), col_3=3:1)

# define a min/2 function following the structure of na.min
na.min_half <- function(.x, ...)
  {
  na.replace(.x, .na = function(x) min(x/2, na.rm = TRUE))
}

# output 
>tbl
#col_1 col_2 col_3
1     a     1     3
2     b    NA     2
3     c     3     1

impute_at(.tbl = tbl,
          .na = na.min,
          .vars = 2)
 #col_1 col_2 col_3
1     a     1     3
2     b     1     2
3     c     3     1

impute_at(.tbl = tbl,
          .na = na.min_half,
          .vars = 2)
#col_1 col_2 col_3
1     a   1.0     3
2     b   0.5     2
3     c   3.0     1
1 Like

Thank you so much, it worked!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.