Loop to get quantiles

My subsample

structure(list(peso1_v00 = structure(c(97.8, 102.9, 78.4, 80.4, 
82.5, 92.6, 99, 66.8, 91.8, 70), label = "Peso: 1a determinación", format.spss = "F5.1"), 
    cintura1_v00 = structure(c(120, 123.2, 104, 106, 117.7, 115, 
    123, 93.5, 116.7, 109), label = "Cintura: 1a determinación", format.spss = "F5.1"), 
    tasis2_e_v00 = structure(c(139, 123, 138, 143, 160, 167, 
    139, 134, 161, 145), label = "TA: tensión arterial 2: sistólica", format.spss = "F4.0"), 
    tadias2_e_v00 = structure(c(74, 65, 75, 55, 75, 76, 65, 64, 
    62, 79), label = "TA: tensión arterial 2: diastólica", format.spss = "F4.0"), 
    p17_total_v00 = structure(c(7, 5, 9, 5, 8, 9, 4, 10, 10, 
    10), label = "Cuestionario de 17 puntos: Suma de puntuación de P17", format.spss = "F3.0"), 
    geaf_tot_v00 = structure(c(0, 1286.71, 5524.48, 419.58, 198.14, 
    839.16, 3188.81, 993.94, 2386.01, 3636.36), label = "AF: Gasto energético en actividad física total (MET•min/sem)", format.spss = "F8.2"), 
    glucosa_v00 = structure(c(116, 152, 113, 257, 108, 119, 108, 
    112, 141, 102), label = "Analítica: Glucosa en mg/dL", format.spss = "F4.0"), 
    albumi_v00 = structure(c(4.57, 4.71, 4.85, 4.43, 4.44, 4.59, 
    4.6, 4.56, 4.45, 4.59), label = "Analítica: Albúmina en g/dL", format.spss = "F6.2"), 
    coltot_v00 = structure(c(261, 197, 235, 168, 217, 248, 155, 
    254, 265, 326), label = "Analítica: Colesterol total en mg/dL", format.spss = "F4.0"), 
    hdl_v00 = structure(c(71, 43, 38, 36, 57, 49, 54, 60, 48, 
    60), label = "Analítica: Colesterol HDL en mg/dL", format.spss = "F4.0"), 
    ldl_calc_v00 = structure(c(147, 135, 161, NA, 145, 155, 73, 
    157, 172, 222), label = "Analítica: LDL calculado en mg/dL si trigli<=300", format.spss = "F4.0"), 
    trigli_v00 = structure(c(217, 93, 179, 315, 73, 219, 138, 
    186, 223, 220), label = "Analítica: Triglicéridos en mg/dL", format.spss = "F5.0"), 
    hba1c_v00 = structure(c(6.29, NA, 5.63, 12.17, NA, 6.11, 
    5.9, 5.68, NA, 6.16), label = "Analítica: Hemoglobina glicosilada (HbA1c %)", format.spss = "F5.2"), 
    i_hucpeptide_v00 = structure(c(854.34, NA, 1485.59, 4241.95, 
    NA, 847.89, 1524.39, 1265.48, NA, 290.22), label = "Hu C-peptide (72) IMIM S'han substituit en les següents var els codis de inf i sup a limit de detecció per el limit inf i sup de detecció", format.spss = "F9.2", display_width = 13L), 
    i_hughrelin_v00 = structure(c(681.94, NA, 480.11, 1587.73, 
    NA, 453, 263.93, 392.98, NA, 1327.91), label = "Hu Ghrelin (26) IMIM", format.spss = "F7.2", display_width = 10L), 
    i_hugip_v00 = structure(c(2.67, NA, 2.67, 2.67, NA, 2.67, 
    2.67, 2.67, NA, 2.67), label = "Hu GIP (14) IMIM", format.spss = "F9.2", display_width = 9L), 
    i_huglp1_v00 = structure(c(177.37, NA, 202.62, 519.02, NA, 
    200.13, 163.82, 20.29, NA, 14.14), label = "Hu GLP-1 (27) IMIM", format.spss = "F9.2", display_width = 9L), 
    i_huglucagon_v00 = structure(c(387.58, NA, 591.73, 855.29, 
    NA, 726.99, 430.35, 389.59, NA, 336.04), label = "Hu Glucagon (15) IMIM", format.spss = "F9.2", display_width = 11L), 
    i_huinsulin_v00 = structure(c(278.72, NA, 538.29, 1693.25, 
    NA, 299.75, 608.35, 397.7, NA, 129.17), label = "Hu Insulin (12) IMIM", format.spss = "F7.2", display_width = 10L), 
    i_huleptin_v00 = structure(c(2518.28, NA, 12175.88, 12369.5, 
    NA, 8409.76, 5998.71, 9298.52, NA, 5919.57), label = "Hu Leptin (78) IMIM", format.spss = "F9.2", display_width = 9L), 
    i_hupai1_v00 = structure(c(3084.08, NA, 2650.85, 3202.18, 
    NA, 3085.25, 3410.73, 3109.79, NA, 1375.07), label = "Hu PAI-1 (61) IMIM", format.spss = "F7.2"), 
    i_huresistin_v00 = structure(c(4758.94, NA, 3594.11, 13564.63, 
    NA, 3221.72, 2864.01, 3630.63, NA, 2827.01), label = "Hu Resistin (65) IMIM", format.spss = "F8.2", display_width = 9L), 
    i_huvisfatin_v00 = structure(c(8.64, NA, 2081.59, 2363.58, 
    NA, 2989.72, 653.96, 1129.24, NA, 631.11), label = "Hu Visfatin (22) IMIM", format.spss = "F9.2", display_width = 6L), 
    col_rema_v00 = structure(c(43, 19, 36, NA, 15, 44, 28, 37, 
    45, 44), format.spss = "F8.2", display_width = 14L), homa_v00 = structure(c(1436.96, 
    NA, 2703.41, 19340.68, NA, 1585.34, 2920.08, 1979.66, NA, 
    585.57), format.spss = "F8.2", display_width = 10L), ln_trigli_v00 = structure(c(5.38, 
    4.53, 5.19, 5.75, 4.29, 5.39, 4.93, 5.23, 5.41, 5.39), format.spss = "F8.2", display_width = 15L), 
    ln_homa_v00 = structure(c(7.27, NA, 7.9, 9.87, NA, 7.37, 
    7.98, 7.59, NA, 6.37), format.spss = "F8.2", display_width = 13L), 
    ln_hba1c_v00 = structure(c(1.84, NA, 1.73, 2.5, NA, 1.81, 
    1.77, 1.74, NA, 1.82), format.spss = "F8.2", display_width = 14L), 
    ln_geaf_tot_v00 = structure(c(NA, 7.16, 8.62, 6.04, 5.29, 
    6.73, 8.07, 6.9, 7.78, 8.2), format.spss = "F8.2", display_width = 17L)), row.names = c(NA, 
-10L), class = "data.frame")

What I want to carry out is detection of points outside a certain range

colvars <- c("peso1_v00", "cintura1_v00", "tasis2_e_v00" , "tadias2_e_v00" ,   "p17_total_v00", "geaf_tot_v00"  ,   "glucosa_v00", "albumi_v00", "coltot_v00", "hdl_v00", "ldl_calc_v00", "trigli_v00", "hba1c_v00", "i_hucpeptide_v00", "i_hughrelin_v00", "i_hugip_v00", "i_huglp1_v00", "i_huglucagon_v00", "i_huinsulin_v00", "i_huleptin_v00", "i_hupai1_v00", "i_huresistin_v00", "i_huvisfatin_v00", "col_rema_v00", "homa_v00",  "ln_trigli_v00", "ln_homa_v00", "ln_hba1c_v00", "ln_geaf_tot_v00")

i=1
for (i in 1:length(colvars)) {
  
  q<-sapply(outliers_v00[ ,i], quantile, probs = c(0.25, 0.75), na.rm =T)
  iqr<-sapply(outliers_v00[ ,i], IQR, na.rm =T)
  up <-  q[ , i[2]]+1.5*iqr # Upper Range  
  low<- q[ ,i[1]]-1.5*iqr # Lower Range
  remained<- subset(outliers, outliers[ ,i] > (q[1] - 1.5*iqr) & outliers[ ,i] < (q[2]+1.5*iqr))
  eliminated<-subset(outliers_v00, outliers_v00[,i] < (q[1] - 1.5*iqr) & outliers_v00[,i] > (q[2]+1.5*iqr))

}

Even simplifyng the loop till

i=1
for (i in 1:length(colvars)) {
  
  q<-sapply(outliers_v00[ ,i], quantile, probs = c(0.25, 0.75), na.rm =T)
  iqr<-sapply(outliers_v00[ ,i], IQR, na.rm =T)
}

I don't get both quantiles, in fact I get the same value (not correspondant with anything) for both Q25 and Q75

Without loop I get the vector with values

  q<-sapply(outliers_v00[ ,i], quantile, probs = c(0.25, 0.75), na.rm =T)

But afterwards for the expression remained and eliminates I could not make the iteration work. I know there are alternatives, but I want to learn how to refer to columns [, i]

gives me

> q
    [,1]  [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
25% 97.8 102.9 78.4 80.4 82.5 92.6   99 66.8 91.8    70
75% 97.8 102.9 78.4 80.4 82.5 92.6   99 66.8 91.8    70

That doesn't accord with

quantile(dat[ ,1], probs = c(0.25, 0.75), na.rm = TRUE)
 25%  75% 
78.9 96.5

Here's how I'd do it rather than with a loop

get_2_quants <- function(x) quantile(dat[ ,x], probs = c(0.25, 0.75), na.rm = TRUE)

dat <- structure(list(
  peso1_v00 = structure(c(
    97.8, 102.9, 78.4, 80.4,
    82.5, 92.6, 99, 66.8, 91.8, 70
  ), label = "Peso: 1a determinación", format.spss = "F5.1"),
  cintura1_v00 = structure(c(
    120, 123.2, 104, 106, 117.7, 115,
    123, 93.5, 116.7, 109
  ), label = "Cintura: 1a determinación", format.spss = "F5.1"),
  tasis2_e_v00 = structure(c(
    139, 123, 138, 143, 160, 167,
    139, 134, 161, 145
  ), label = "TA: tensión arterial 2: sistólica", format.spss = "F4.0"),
  tadias2_e_v00 = structure(c(
    74, 65, 75, 55, 75, 76, 65, 64,
    62, 79
  ), label = "TA: tensión arterial 2: diastólica", format.spss = "F4.0"),
  p17_total_v00 = structure(c(
    7, 5, 9, 5, 8, 9, 4, 10, 10,
    10
  ), label = "Cuestionario de 17 puntos: Suma de puntuación de P17", format.spss = "F3.0"),
  geaf_tot_v00 = structure(c(
    0, 1286.71, 5524.48, 419.58, 198.14,
    839.16, 3188.81, 993.94, 2386.01, 3636.36
  ), label = "AF: Gasto energético en actividad física total (MET•min/sem)", format.spss = "F8.2"),
  glucosa_v00 = structure(c(
    116, 152, 113, 257, 108, 119, 108,
    112, 141, 102
  ), label = "Analítica: Glucosa en mg/dL", format.spss = "F4.0"),
  albumi_v00 = structure(c(
    4.57, 4.71, 4.85, 4.43, 4.44, 4.59,
    4.6, 4.56, 4.45, 4.59
  ), label = "Analítica: Albúmina en g/dL", format.spss = "F6.2"),
  coltot_v00 = structure(c(
    261, 197, 235, 168, 217, 248, 155,
    254, 265, 326
  ), label = "Analítica: Colesterol total en mg/dL", format.spss = "F4.0"),
  hdl_v00 = structure(c(
    71, 43, 38, 36, 57, 49, 54, 60, 48,
    60
  ), label = "Analítica: Colesterol HDL en mg/dL", format.spss = "F4.0"),
  ldl_calc_v00 = structure(c(
    147, 135, 161, NA, 145, 155, 73,
    157, 172, 222
  ), label = "Analítica: LDL calculado en mg/dL si trigli<=300", format.spss = "F4.0"),
  trigli_v00 = structure(c(
    217, 93, 179, 315, 73, 219, 138,
    186, 223, 220
  ), label = "Analítica: Triglicéridos en mg/dL", format.spss = "F5.0"),
  hba1c_v00 = structure(c(
    6.29, NA, 5.63, 12.17, NA, 6.11,
    5.9, 5.68, NA, 6.16
  ), label = "Analítica: Hemoglobina glicosilada (HbA1c %)", format.spss = "F5.2"),
  i_hucpeptide_v00 = structure(c(
    854.34, NA, 1485.59, 4241.95,
    NA, 847.89, 1524.39, 1265.48, NA, 290.22
  ), label = "Hu C-peptide (72) IMIM S'han substituit en les següents var els codis de inf i sup a limit de detecció per el limit inf i sup de detecció", format.spss = "F9.2", display_width = 13L),
  i_hughrelin_v00 = structure(c(
    681.94, NA, 480.11, 1587.73,
    NA, 453, 263.93, 392.98, NA, 1327.91
  ), label = "Hu Ghrelin (26) IMIM", format.spss = "F7.2", display_width = 10L),
  i_hugip_v00 = structure(c(
    2.67, NA, 2.67, 2.67, NA, 2.67,
    2.67, 2.67, NA, 2.67
  ), label = "Hu GIP (14) IMIM", format.spss = "F9.2", display_width = 9L),
  i_huglp1_v00 = structure(c(
    177.37, NA, 202.62, 519.02, NA,
    200.13, 163.82, 20.29, NA, 14.14
  ), label = "Hu GLP-1 (27) IMIM", format.spss = "F9.2", display_width = 9L),
  i_huglucagon_v00 = structure(c(
    387.58, NA, 591.73, 855.29,
    NA, 726.99, 430.35, 389.59, NA, get_2_quants <- function(x) glue::glue(x,quantile(dat[ ,x], probs = c(0.25, 0.75), na.rm = TRUE)
336.04
  ), label = "Hu Glucagon (15) IMIM", format.spss = "F9.2", display_width = 11L),
  i_huinsulin_v00 = structure(c(
    278.72, NA, 538.29, 1693.25,
    NA, 299.75, 608.35, 397.7, NA, 129.17
  ), label = "Hu Insulin (12) IMIM", format.spss = "F7.2", display_width = 10L),
  i_huleptin_v00 = structure(c(
    2518.28, NA, 12175.88, 12369.5,
    NA, 8409.76, 5998.71, 9298.52, NA, 5919.57
  ), label = "Hu Leptin (78) IMIM", format.spss = "F9.2", display_width = 9L),
  i_hupai1_v00 = structure(c(
    3084.08, NA, 2650.85, 3202.18,
    NA, 3085.25, 3410.73, 3109.79, NA, 1375.07
  ), label = "Hu PAI-1 (61) IMIM", format.spss = "F7.2"),
  i_huresistin_v00 = structure(c(
    4758.94, NA, 3594.11, 13564.63,
    NA, 3221.72, 2864.01, 3630.63, NA, 2827.01
  ), label = "Hu Resistin (65) IMIM", format.spss = "F8.2", display_width = 9L),
  i_huvisfatin_v00 = structure(c(
    8.64, NA, 2081.59, 2363.58,
    NA, 2989.72, 653.96, 1129.24, NA, 631.11
  ), label = "Hu Visfatin (22) IMIM", format.spss = "F9.2", display_width = 6L),
  col_rema_v00 = structure(c(
    43, 19, 36, NA, 15, 44, 28, 37,
    45, 44
  ), format.spss = "F8.2", display_width = 14L), homa_v00 = structure(c(
    1436.96,
    NA, 2703.41, 19340.68, NA, 1585.34, 2920.08, 1979.66, NA,
    585.57
  ), format.spss = "F8.2", display_width = 10L), ln_trigli_v00 = structure(c(
    5.38,
    4.53, 5.19, 5.75, 4.29, 5.39, 4.93, 5.23, 5.41, 5.39
  ), format.spss = "F8.2", display_width = 15L),
  ln_homa_v00 = structure(c(
    7.27, NA, 7.9, 9.87, NA, 7.37,
    7.98, 7.59, NA, 6.37
  ), format.spss = "F8.2", display_width = 13L),
  ln_hba1c_v00 = structure(c(
    1.84, NA, 1.73, 2.5, NA, 1.81,
    1.77, 1.74, NA, 1.82
  ), format.spss = "F8.2", display_width = 14L),
  ln_geaf_tot_v00 = structure(c(
    NA, 7.16, 8.62, 6.04, 5.29,
    6.73, 8.07, 6.9, 7.78, 8.2
  ), format.spss = "F8.2", display_wget_2_quants <- function(x) glue::glue(x,quantile(dat[ ,x], probs = c(0.25, 0.75), na.rm = TRUE)
idth = 17L)
), row.names = c(
  NA,
  -10L
), class = "data.frame")


lapply(seq_along(colnames(dat)),get_2_quants)

So, as to for loops, which should only be necessary with more than a single level of indexing.

First, initialize a receiver object in which to save iterations into the global environment

holder <- list()

The basic pattern will be

for(i in SOMETHING) holder[i] = VALUE_OF_SOME_OPERATION

In this case, let's use a revised colvars and a user function for the purpose

colvars <- colnames(outliers_v00) # depends on knowing all 
                                                                       # columns are numeric
                                                                       # and we want them all
                                                                      # avoids copying errors
                                                                     # alternatively, subset
holder <- list()
get_2_quants <- function(x,y) quantile(x[ ,y], probs = c(0.25, 0.75), na.rm = TRUE)
for (i in seq_along(colvars)) holder[i] = list(get_2_quants(outliers_v00,i))

which will yield (reprex doesn't like the data object, so I'm cutting and pasting) a list of lists

> holder
[[1]]
 25%  75% 
78.9 96.5 

[[2]]
    25%     75% 
106.750 119.425 

[[3]]
   25%    75% 
138.25 156.25 

[[4]]
  25%   75% 
64.25 75.00 

[[5]]
 25%  75% 
5.50 9.75 

[[6]]
     25%      75% 
 524.475 2988.110 

[[7]]
  25%   75% 
109.0 135.5 

[[8]]
   25%    75% 
4.4775 4.5975 

[[9]]
   25%    75% 
202.00 259.25 

[[10]]
  25%   75% 
44.25 59.25 

[[11]]
25% 75% 
145 161 

[[12]]
   25%    75% 
148.25 219.75 

[[13]]
  25%   75% 
5.790 6.225 

[[14]]
     25%      75% 
 851.115 1504.990 

[[15]]
     25%      75% 
 422.990 1004.925 

[[16]]
 25%  75% 
2.67 2.67 

[[17]]
    25%     75% 
 92.055 201.375 

[[18]]
    25%     75% 
388.585 659.360 

[[19]]
    25%     75% 
289.235 573.320 

[[20]]
     25%      75% 
 5959.14 10737.20 

[[21]]
     25%      75% 
2867.465 3155.985 

[[22]]
     25%      75% 
3042.865 4194.785 

[[23]]
     25%      75% 
 642.535 2222.585 

[[24]]
25% 75% 
 28  44 

[[25]]
     25%      75% 
1511.150 2811.745 

[[26]]
  25%   75% 
4.995 5.390 

[[27]]
 25%  75% 
7.32 7.94 

[[28]]
  25%   75% 
1.755 1.830 

[[29]]
 25%  75% 
6.73 8.07

The first line of code (I guess) tries to convert2list the object. It is not working the way you posted it.

You create a function to get quantiles that works perfectly, but how do I get IQR in the loop and put it together with the list of quantiles to make up the formula

remained<- subset(outliers, outliers[ ,i] > (q[1] - 1.5*iqr) & outliers[ ,i] < (q[2]+1.5*iqr))
  eliminated<-subset(outliers_v00, outliers_v00[,i] < (q[1] - 1.5*iqr) & outliers_v00[,i] > (q[2]+1.5*iqr))

I'm unsure of your intent, now: to find outliers within a column or across a row. Assuming column, consider (again, no reprex due to trouble with input)

dat <- structure(list(peso1_v00 = structure(c(97.8, 102.9, 78.4, 80.4, 
+ 82.5, 92.6, 99, 66.8, 91.8, 70), label = "Peso: 1a determinación", format.spss = "F5.1"), 
+ cintura1_v00 = structure(c(120, 123.2, 104, 106, 117.7, 115, 
+ 123, 93.5, 116.7, 109), label = "Cintura: 1a determinación", format.spss = "F5.1"), 
+ tasis2_e_v00 = structure(c(139, 123, 138, 143, 160, 167, 
+ 139, 134, 161, 145), label = "TA: tensión arterial 2: sistólica", format.spss = "F4.0"), 
+ tadias2_e_v00 = structure(c(74, 65, 75, 55, 75, 76, 65, 64, 
+ 62, 79), label = "TA: tensión arterial 2: diastólica", format.spss = "F4.0"), 
+ p17_total_v00 = structure(c(7, 5, 9, 5, 8, 9, 4, 10, 10, 
+ 10), label = "Cuestionario de 17 puntos: Suma de puntuación de P17", format.spss = "F3.0"), 
+ geaf_tot_v00 = structure(c(0, 1286.71, 5524.48, 419.58, 198.14, 
+ 839.16, 3188.81, 993.94, 2386.01, 3636.36), label = "AF: Gasto energético en actividad física total (MET•min/sem)", format.spss = "F8.2"), 
+ glucosa_v00 = structure(c(116, 152, 113, 257, 108, 119, 108, 
+ 112, 141, 102), label = "Analítica: Glucosa en mg/dL", format.spss = "F4.0"), 
+ albumi_v00 = structure(c(4.57, 4.71, 4.85, 4.43, 4.44, 4.59, 
+ 4.6, 4.56, 4.45, 4.59), label = "Analítica: Albúmina en g/dL", format.spss = "F6.2"), 
+ coltot_v00 = structure(c(261, 197, 235, 168, 217, 248, 155, 
+ 254, 265, 326), label = "Analítica: Colesterol total en mg/dL", format.spss = "F4.0"), 
+ hdl_v00 = structure(c(71, 43, 38, 36, 57, 49, 54, 60, 48, 
+ 60), label = "Analítica: Colesterol HDL en mg/dL", format.spss = "F4.0"), 
+ ldl_calc_v00 = structure(c(147, 135, 161, NA, 145, 155, 73, 
+ 157, 172, 222), label = "Analítica: LDL calculado en mg/dL si trigli<=300", format.spss = "F4.0"), 
+ trigli_v00 = structure(c(217, 93, 179, 315, 73, 219, 138, 
+ 186, 223, 220), label = "Analítica: Triglicéridos en mg/dL", format.spss = "F5.0"), 
+ hba1c_v00 = structure(c(6.29, NA, 5.63, 12.17, NA, 6.11, 
+ 5.9, 5.68, NA, 6.16), label = "Analítica: Hemoglobina glicosilada (HbA1c %)", format.spss = "F5.2"), 
+ i_hucpeptide_v00 = structure(c(854.34, NA, 1485.59, 4241.95, 
+ NA, 847.89, 1524.39, 1265.48, NA, 290.22), label = "Hu C-peptide (72) IMIM S'han substituit en les següents var els codis de inf i sup a limit de detecció per el limit inf i sup de detecció", format.spss = "F9.2", display_width = 13L), 
+ i_hughrelin_v00 = structure(c(681.94, NA, 480.11, 1587.73, 
+ NA, 453, 263.93, 392.98, NA, 1327.91), label = "Hu Ghrelin (26) IMIM", format.spss = "F7.2", display_width = 10L), 
+ i_hugip_v00 = structure(c(2.67, NA, 2.67, 2.67, NA, 2.67, 
+ 2.67, 2.67, NA, 2.67), label = "Hu GIP (14) IMIM", format.spss = "F9.2", display_width = 9L), 
+ i_huglp1_v00 = structure(c(177.37, NA, 202.62, 519.02, NA, 
+ 200.13, 163.82, 20.29, NA, 14.14), label = "Hu GLP-1 (27) IMIM", format.spss = "F9.2", display_width = 9L), 
+ i_huglucagon_v00 = structure(c(387.58, NA, 591.73, 855.29, 
+ NA, 726.99, 430.35, 389.59, NA, 336.04), label = "Hu Glucagon (15) IMIM", format.spss = "F9.2", display_width = 11L), 
+ i_huinsulin_v00 = structure(c(278.72, NA, 538.29, 1693.25, 
+ NA, 299.75, 608.35, 397.7, NA, 129.17), label = "Hu Insulin (12) IMIM", format.spss = "F7.2", display_width = 10L), 
+ i_huleptin_v00 = structure(c(2518.28, NA, 12175.88, 12369.5, 
+ NA, 8409.76, 5998.71, 9298.52, NA, 5919.57), label = "Hu Leptin (78) IMIM", format.spss = "F9.2", display_width = 9L), 
+ i_hupai1_v00 = structure(c(3084.08, NA, 2650.85, 3202.18, 
+ NA, 3085.25, 3410.73, 3109.79, NA, 1375.07), label = "Hu PAI-1 (61) IMIM", format.spss = "F7.2"), 
+ i_huresistin_v00 = structure(c(4758.94, NA, 3594.11, 13564.63, 
+ NA, 3221.72, 2864.01, 3630.63, NA, 2827.01), label = "Hu Resistin (65) IMIM", format.spss = "F8.2", display_width = 9L), 
+ i_huvisfatin_v00 = structure(c(8.64, NA, 2081.59, 2363.58, 
+ NA, 2989.72, 653.96, 1129.24, NA, 631.11), label = "Hu Visfatin (22) IMIM", format.spss = "F9.2", display_width = 6L), 
+ col_rema_v00 = structure(c(43, 19, 36, NA, 15, 44, 28, 37, 
+ 45, 44), format.spss = "F8.2", display_width = 14L), homa_v00 = structure(c(1436.96, 
+ NA, 2703.41, 19340.68, NA, 1585.34, 2920.08, 1979.66, NA, 
+ 585.57), format.spss = "F8.2", display_width = 10L), ln_trigli_v00 = structure(c(5.38, 
+ 4.53, 5.19, 5.75, 4.29, 5.39, 4.93, 5.23, 5.41, 5.39), format.spss = "F8.2", display_width = 15L), 
+ ln_homa_v00 = structure(c(7.27, NA, 7.9, 9.87, NA, 7.37, 
+ 7.98, 7.59, NA, 6.37), format.spss = "F8.2", display_width = 13L), 
+ ln_hba1c_v00 = structure(c(1.84, NA, 1.73, 2.5, NA, 1.81, 
+ 1.77, 1.74, NA, 1.82), format.spss = "F8.2", display_width = 14L), 
+ ln_geaf_tot_v00 = structure(c(NA, 7.16, 8.62, 6.04, 5.29, 
+ 6.73, 8.07, 6.9, 7.78, 8.2), format.spss = "F8.2", display_width = 17L)), row.names = c(NA, 
+ -10L), class = "data.frame")
> 
> spread <- 1.50
> 
> find_hi <- function(x){
+   obj = dat[,x]
+   my_iqr   <- quantile(obj, na.rm = TRUE)[4] - quantile(obj, na.rm = TRUE)[2]
+   upper    <- quantile(obj, na.rm = TRUE)[4] + my_iqr * spread
+   lower    <- quantile(obj, na.rm = TRUE)[2] - my_iqr * spread
+   too_high <- which(obj >= upper)
+   return(c(too_high))
+ }
> 
> find_lo <- function(x){
+   obj = dat[,x]
+   my_iqr   <- quantile(obj, na.rm = TRUE)[4] - quantile(obj, na.rm = TRUE)[2]
+   upper    <- quantile(obj, na.rm = TRUE)[4] + my_iqr * spread
+   lower    <- quantile(obj, na.rm = TRUE)[2] - my_iqr * spread
+   too_low <- which(obj <= lower)
+   return(c(too_low))
+ }
> 
> 
> hi_rows  <- unique(unlist(lapply(1:29, find_hi)))
> low_rows <- unique(unlist(lapply(1:29, find_lo)))
> out_rows <- union(hi_rows,low_rows)
> keep_rows <- setdiff(1:10,out_rows)
> dat[keep_rows,]
# peso1_v00 cintura1_v00 tasis2_e_v00 tadias2_e_v00 p17_total_v00 geaf_tot_v00
# 2     102.9        123.2          123            65             5      1286.71
# 9      91.8        116.7          161            62            10      2386.01
# glucosa_v00 albumi_v00 coltot_v00 hdl_v00 ldl_calc_v00 trigli_v00 hba1c_v00
# 2         152       4.71        197      43          135         93        NA
# 9         141       4.45        265      48          172        223        NA
# i_hucpeptide_v00 i_hughrelin_v00 i_hugip_v00 i_huglp1_v00 i_huglucagon_v00
# 2               NA              NA          NA           NA               NA
# 9               NA              NA          NA           NA               NA
# i_huinsulin_v00 i_huleptin_v00 i_hupai1_v00 i_huresistin_v00 i_huvisfatin_v00
# 2              NA             NA           NA               NA               NA
# 9              NA             NA           NA               NA               NA
# col_rema_v00 homa_v00 ln_trigli_v00 ln_homa_v00 ln_hba1c_v00 ln_geaf_tot_v00
# 2           19       NA          4.53          NA           NA            7.16
# 9           45       NA          5.41          NA           NA            7.78