[R] Using Partial Column Matching for Mutate

Shawn Way SW@y @end|ng |rom meco@com
Tue Aug 6 14:33:50 CEST 2019


I have a tibble that has a large number of variables and because I'm partial lazy (and I really want to know how to do this), I would like find out if it possible to partial column matching with the mutate function in the tidyverse.

I have a tibble with the following

> gross_test  <- gross_df %>%
+     select(Job,Mfg_Labor_Hrs_Planned,Mfg_Labor_Hrs_Actual,Eng_Labor_Hrs_Planned,Eng_Labor_Hrs_Actual)
> gross_test
# A tibble: 6 x 5
    Job Mfg_Labor_Hrs_Pla~ Mfg_Labor_Hrs_Ac~ Eng_Labor_Hrs_Pl~ Eng_Labor_Hrs_Ac~
* <dbl>              <dbl>             <dbl>             <dbl>             <dbl>
1  9892               950.             1082.               133              302.
2  9893               950.             1082.               133              302.
3  9652               950.             1082.               133              302.
4  9894               950.             1082.               133              302.
5  9652               950.             1082.               133              302.
6  9894               950.             1082.               133              302.
>

The column names follow a pattern of (.+)_Planned and (.+)_Actual and there are a large number of them.  What I would like to do is the following:

mutate(Mfg_Labor_Hrs_Diff= Mfg_Labor_Hrs_Actual- Mfg_Labor_Hrs_Planned)

using a regex.  Something like the following

mutate( $1_Diff = $1_Actual - $1_Planned)

Where $1 was Mfg_Labor_Hrs.  This would iterate over the entire set of columns doing Mfg_Labor_Hrs and then Eng_Labor_Hrs, etc.

Is this even possible or will I need to explicitly write out the mutate for each combination?

Thanks for looking at this.

Shawn Way, P.E.

   



More information about the R-help mailing list