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:
Hi R community,
I have a data frame with three variables, where each row adds up to 90.
I want to assign a category of low, medium, or high to the values in each
row - where the lowest value per row will be set to 10, the medium value
set to 30, and the high value set to 50 - so each row still adds up to 90.
For example:
Data: Orig
tree shrub grass
32 11 47
23 41 26
49 23 18
Data: New
tree shrub grass
30 10 50
10 50 30
50 30 10
I am not attaching any code here as I have not been able to write anything
effective! appreciate help with this!
thank you,
JC
--
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.