[R] categorizing data
Richard O'Keefe
r@oknz @end|ng |rom gm@||@com
Mon May 30 07:07:25 CEST 2022
Suppose your data are in a table called plant.data
Suppose you want to process row i.
Then plant.data[i,] is a vector of 3 numbers.
ord <- order(plant.data[i,])
gives you a vector of 3 positive integers such
that plant.data[i,ord] is in ascending order.
plant.data[i,ord[1]] <- 10
plant.data[i,ord[2]] <- 30
plant.data[i,ord[3]] <- 50
or even
plant.data[i,ord] <- c(10,30,50)
Wrapping it up and tying a bow on it:
new.values <- c(10,30,50)
for (i in 1:nrow(plant.data))
plant.data[i,order(plant.data[i,])] <- new.values
> plant.orig <- data.frame(
+ tree = c(32,23,49),
+ shrub = c(11,41,23),
+ grass = c(47,26,18))
> plant.orig
tree shrub grass
1 32 11 47
2 23 41 26
3 49 23 18
> new.values <- c(10,30,50)
> plant.new <- plant.orig
> for (i in 1:nrow(plant.new))
+ plant.new[i,order(plant.new[i,])] <- new.values
> plant.new
tree shrub grass
1 30 10 50
2 10 50 30
3 50 30 10
On Mon, 30 May 2022 at 07:29, Janet Choate <jsc.eco using 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 using 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list