Skip to content
Prev 391763 / 398498 Next

categorizing data

You could write a function that deals with one row of your data, based on
the order() function.  E.g.,
  > to_10_30_50
  function(x) {
    stopifnot(is.numeric(x), length(x)==3, sum(x)==90, all(x>0))
    c(10,30,50)[order(x)]
  }
  <bytecode: 0x000001912dcd1bd8>
  > to_10_30_50(c(23,41,26))
  [1] 10 50 30
Then loop over the rows.  Since this is a data.frame and not a matrix, you
need to coerce each row from a single-row data.frame to a numeric vector:
  > data <- data.frame(tree=c(32,23,49), shrub=c(11,41,23),
grass=c(47,26,18))
  > for(i in 1:nrow(new)) data[i,] <- to_10_30_50(as.numeric(data[i,]))
  > data
    tree shrub grass
  1   30    10    50
  2   10    50    30
  3   50    30    10

-Bill
On Sun, May 29, 2022 at 12:29 PM Janet Choate <jsc.eco at gmail.com> wrote: