Search code examples
rif-statementrecode

Is there a way to let biggest value on X (out of X, Y, Z) decide value on V in R?


I have a dataset (ft.mutate.topics) with five variables (four numeric are ft_technical, ft_performative, ft_procedural and ft_moral). The fifth is "topic_lab" and I would like for it to take on the name (as a character) related to the variable with the highest value of the four other.

The below produces a dataset similar to mine.

set.seed(1)
Data <- data.frame(
X = sample(1:10),
Y = sample(1:10),
Z = sample(1:10))

The I would like for a variable - V - to take on either "X", "Y", og "Z" for each observation corresponding to which of these three variables, that takes on the highest value - as an example for X, this is similar again:

if (Data$X > Data$Y & Data$X > Data$Z) Data$label <- "X"

Warning message:
In if (Data$X > Data$Y & Data$X > Data$Z) Data$label <- "X": 
the condition has length > 1 and only the first element will be used    

In relation to my initial example I have tried the following with a combination of if-commands:

if (ft.mutate.topics$ft_technical > ft.mutate.topics$ft_performative &
    ft.mutate.topics$ft_technical > ft.mutate.topics$ft_procedural &
    ft.mutate.topics$ft_technical > ft.mutate.topics$ft_moral)
  ft.mutate.topics$topic_lab = "technical"

if (ft.mutate.topics$ft_performative > ft.mutate.topics$ft_technical &
    ft.mutate.topics$ft_performative > ft.mutate.topics$ft_procedural &
    ft.mutate.topics$ft_performative > ft.mutate.topics$ft_moral)
  ft.mutate.topics$topic_lab = "performative"

if (ft.mutate.topics$ft_procedural > ft.mutate.topics$ft_performative &
    ft.mutate.topics$ft_procedural > ft.mutate.topics$ft_technical &
    ft.mutate.topics$ft_procedural > ft.mutate.topics$ft_moral)
  ft.mutate.topics$topic_lab = "procedural"

if (ft.mutate.topics$ft_moral > ft.mutate.topics$ft_performative &
    ft.mutate.topics$ft_moral > ft.mutate.topics$ft_procedural &
    ft.mutate.topics$ft_moral > ft.mutate.topics$ft_technical)
  ft.mutate.topics$topic_lab = "moral"

It says: "the condition has length > 1 and only the first element will be used" and substitutes the whole variable with "performative" because it is has the highest value in row 1. Anybody know what is up?

Thank you!


Solution

  • This seems simple. I will use a made up dataset, to adapt to yours should be easy.

    nms <- sub("^ft_", "", names(ft))
    ft$topic.lab <- apply(ft, 1, function(x) nms[which.max(x)])
    

    Data.

    This is a simulated dataset.

    set.seed(1234)
    n <- 20
    ft <- data.frame(ft_X = rnorm(n, 0, 2),
                     ft_Y = rnorm(n, 0, 3),
                     ft_Z = rnorm(n, 0, 4))