case_when mutate question

name location total_cats total_cats_down total_cats_up total_cats_updown total_cats_downup total_cats_oe total_cats_ce ... total_cats_downup what_col
 
Annie NJ          44            23            33               33               39            99              33                  88        total_cats_updown
Ryan  BA          34            33            11               23               23            12              39                  11        total_cats

above is my data frame. I have the columns total_cats, total_cats_down, total_cats_up, total_cats_updown, total_cats_downup...total_cats_oe, total_cats_ce... and for every other column from total_cats_down:total_cats_downup I have columns that end in _oe and _ce. I did not write them out so it is shorter to view. I want to make three new columns: total_cats_final, total_cats_oe_final, and total_cats_ce_final based on the value in the column that is listed in what_col.

for example, if what_col is total_cats_updown, then :
total_cats_final , total_cats_oe_final , total_cats_ce_final would have the values found in total_cats_updown, total_cats_updown_oe, total_cats_updown_ce

is there a shorter way to do this than just do a case_when + mutate for three new columns in the Tidyverse? this could be very lengthy and repetitive I feel. thank u<3

your example shows total_cats_downup twice, this would be extremely awkward.
is your data really like that , or have you made a mistake in creating your example ?

sorry that is mistake in creating example. it was supposed to be... total_cats_downup_oe, total_cats_downup_ce

I dont know if its shorter than your case statements (though it has potnetial to be greatly reduced if certain variables were guaranteed to be in the frame i.e. total_cats_updown_oe

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
  name = c("NJ", "BA"),
  location = c(44L, 34L),
  total_cats = c(23L, 33L),
  total_cats_down = c(33L, 11L),
  total_cats_up = c(33L, 23L),
  total_cats_updown = c(39L, 23L),
  total_cats_downup = c(99L, 12L),
  total_cats_oe = c(33L, 39L),
  total_cats_ce = c(88L, 11L),
  what_col = c("total_cats_updown", "total_cats")
)


mutate(rowwise(df),
       total_cats_final = cur_data()[[what_col]],
       total_cats_oe_final = {x <- paste0(what_col,"_oe");if(x %in% names(cur_data())){cur_data()[[x]] }else{NA}},
       total_cats_ce_final = {x <- paste0(what_col,"_ce");if(x %in% names(cur_data())){cur_data()[[x]] }else{NA}},
       )
1 Like

thank u this is so incredibly helpful. do u have a recommendation for running a loop on this if I have so many columns I am making "final" versions of? im just changing the suffix and what the new column name would be

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.