vendredi 18 mars 2016

Function with IF statement to apply to multiple columns

I want to apply an IF statement to multiple columns (essentially an entire data frame) and am taking the approach of creating a function to do so. The aim is to replace the data in the columns with a number representing the group that number falls into.

The data sample looks as such:

> Mat
        A    B    C    D    E
E1   8.45 6.65 7.35 5.18 3.11
E2  12.59 4.18 4.08 0.95 1.75
E3  15.93 3.05 1.81 2.77 4.42
E4  15.93 3.05 1.81 2.77 4.42
E5  11.57 4.48 4.70 2.01 1.08
E6   8.17 7.05 7.70 5.38 3.45
E7  11.57 4.48 4.70 2.01 1.08
E8   9.49 5.41 6.51 5.78 3.20
E9  11.71 4.40 4.58 1.87 1.11
E10  9.52 5.49 6.63 6.07 3.49

The function I tried to create will take an IF statement and look at each value in a column and depending on the value replace it with a group number from 1 to 6 (for numbers between 1 and 10) and an NA for numbers greater than 10. The IF statement itself worked when I write it out manually for ONE column. The function I wrote is as such (called Grouping):

# write user function to apply the loop

Grouping = function(data) {
  for(i in 1:length(x)) {
    if(x[i] < 1) {
      x[i] = 1
    } else if (x[i] < 3) {
      x[i] = 3
    } else if (x[i] < 4) {
      x[i] = 4
    } else if (x[i] < 5) {
      x[i] = 5
    } else if (x[i] < 10) {
      x[i] = 6
    } else
      x[i] = "NA"
  }
} 

When I attempted to use apply with the function my error was:

> apply(Mat, 1, Grouping)
Error in FUN(newX[, i], ...) : object 'x' not found

Clearly the problem is in my construction of the user function but I'm not sure where I've gone wrong as I'm quite new to function creation.

Any help is appreciated!

Thanks!

Aucun commentaire:

Enregistrer un commentaire