mardi 13 juillet 2021

Removing Outliers in loop - function will print but not return

I'm writing a function to remove outliers. When I print the results it does what I want but I cant get it to return them. What am I doing wrong? I have a similar code set up that returns the results

When I Print:

NA_Outliers = function(x){
  Q1 <- quantile(x, probs=.25)
  Q3 <- quantile(x, probs=.75)
  iqr = Q3-Q1

  upper_limit = Q3 + (iqr*1.5)
  lower_limit = Q1 - (iqr*1.5)
  for (value in x) {
    if (value> upper_limit | value < lower_limit){
      value = NA
      print(value)
    }else{
      print(value)
    }
  }
}

Results:

> NA_Outliers(Test)
[1] NA
[1] 2.428675
[1] 2.428384
[1] 2.714187
[1] 2.457054
[1] 2.464337
[1] 2.686667
[1] 2.166072
[1] 2.690987
[1] 2.632692
[1] NA
[1] 2.84985

When I Return:

NA_Outliers = function(x){
  Q1 <- quantile(x, probs=.25)
  Q3 <- quantile(x, probs=.75)
  iqr = Q3-Q1

  upper_limit = Q3 + (iqr*1.5)
  lower_limit = Q1 - (iqr*1.5)
  for (value in x) {
    if (value> upper_limit | value < lower_limit){
      value = NA
      return(value)
    }else{
      return(value)
    }
  }
}

Results:

[1] NA

I tried this as well and got the same results

NA_Outliers = function(x){
  Q1 <- quantile(x, probs=.25)
  Q3 <- quantile(x, probs=.75)
  iqr = Q3-Q1

  upper_limit = Q3 + (iqr*1.5)
  lower_limit = Q1 - (iqr*1.5)
  for (value in x) {
    if(class(value) == "numeric"){
      value[value> upper_limit | value < lower_limit] = NA
      return(value)
    }else{
      return(x)
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire