Home > Software engineering >  New column depending on the values of the rest with R
New column depending on the values of the rest with R

Time:06-29

I have a dataframe like this:

enter image description here

In which I have some units of analysis and the percentage of their area that was created in certain years. What I want to do is to create a column that selects the highest value and "tells" me which year it belongs to. For example, in UA-1, 26 is the highest value and the new column will tell me that this unit of analysis is from 1985.

However, I want to add something to this "request": the new column will tell me to which year the highest value belongs as long as it is greater than 25; otherwise, I automatically need it to indicate the year 2005, as in UA-2 and UA-3. So in this example the new column should indicate something like this:

enter image description here

I appreciate any advice.

CodePudding user response:

Here is another solution

units_Of_Analysis <- c("UA-1", "UA-2", "UA-3")
y_1975 <- c(21, 10, 10)
y_1985 <- c(26, 15, 11)
y_1995 <- c(0, 19, 12)
y_2005 <- c(10, 8, 9)

df <- data.frame(units_Of_Analysis, y_1975, y_1985, y_1995, y_2005)
mat <- as.matrix(df[, -1])
col_Names <- colnames(mat)

my_Fun <- function(x, col_Names)
{
  bool_Gt_25 <- x > 25
  
  if(any(bool_Gt_25))
  {
    return(col_Names[which.max(x)])
    
  }else
  {
    return("y_2005")
  }
}

new_Col <- apply(X = mat, MARGIN = 1, FUN = function(x) my_Fun(x, col_Names))
df[["new_Col"]] <- new_Col

CodePudding user response:

library(tidyverse)

df <- data.frame(analysis = paste0("UA-", 1:3),
                 y1975 = c(21, 10, 10),
                 y1985 = c(26, 15, 11),
                 y1995 = c(0, 19, 12),
                 y2005 = c(10, 8, 9))
df %>%
  rowwise() %>%
  mutate(max_val = max(c_across(y1975:y2005)),
         max_col =  names(.[-1])[which.max(c_across(y1975:y2005))]) %>%
  mutate(new_column = ifelse(max_val > 25, max_col, "y2005"))

# A tibble: 3 x 8
# Rowwise: 
  analysis y1975 y1985 y1995 y2005 max_val max_col new_column
  <chr>    <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>   <chr>     
1 UA-1        21    26     0    10      26 y1985   y1985     
2 UA-2        10    15    19     8      19 y1995   y2005     
3 UA-3        10    11    12     9      12 y1995   y2005 

  • Related