Hello,

Here is a way. Define a function to change the values and call it in a apply loop. But Tom's suggestions are more reasonable, you should have a good reason why to change the data.


x <- '
tree  shrub  grass
32     11       47
23      41      26
49      23      18'
orig <- read.table(textConnection(x), header = TRUE)

f <- function(x) {
  stopifnot(length(x) == 3L)
  i_min <- which.min(x)
  i_max <- which.max(x)
  s <- (x[i_min] - 10) + (x[i_max] - 50)
  x[i_min] <- 10
  x[i_max] <- 50
  x[-c(i_min, i_max)] <- x[-c(i_min, i_max)] + s
  x
}

t(apply(orig, 1, f))
#       tree shrub grass
#  [1,]   30    10    50
#  [2,]   10    50    30
#  [3,]   50    30    10


Hope this helps,

Rui Barradas

Às 20:28 de 29/05/2022, Janet Choate escreveu:
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@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.

______________________________________________
R-help@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.

Reply via email to