Skip to contents

[Experimental]

This function sets new row names for a data frame based on a tidy evaluation expression.

Usage

mutateToRownames(.data, expr, .remove = FALSE, .uniqueSep = ".")

Arguments

.data

A data frame.

expr

A tidy evaluation expression specifying the columns to use for the new row names.

.remove

A logical indicating whether to remove the columns used in expr. Default is FALSE. Note that columns are detected with all.vars().

.uniqueSep

A character string to separate duplicate row names when ensuring uniqueness with make.unique(). Default is ".".

Value

A data frame with updated row names.

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union

# if you just wish to make a column (`wt`) into a rowname:
mtcars %>%
    head() %>%
    mutateToRownames(wt)
#>        mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 2.62  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2.875 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 2.32  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 3.215 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 3.44  18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 3.46  18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# simple example of using tidy evaluation
mtcars %>%
    head() %>%
    mutateToRownames(wt + 3*vs)
#>        mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 2.62  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2.875 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 5.32  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 6.215 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 3.44  18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6.46  18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# use .remove = TRUE to get rid of the column if unneeded
mtcars %>%
    mutateToRownames(qsec + (gear / carb) * wt, .remove = TRUE)
#>                   mpg cyl  disp  hp drat vs am
#> 19.08            21.0   6 160.0 110 3.90  0  1
#> 19.895           21.0   6 160.0 110 3.90  0  1
#> 27.89            22.8   4 108.0  93 3.85  1  1
#> 29.085           21.4   6 258.0 110 3.08  1  0
#> 22.18            18.7   8 360.0 175 3.15  0  0
#> 30.6             18.1   6 225.0 105 2.76  1  0
#> 18.5175          14.3   8 360.0 245 3.21  0  0
#> 26.38            24.4   4 146.7  62 3.69  1  0
#> 29.2             22.8   4 140.8  95 3.92  1  0
#> 21.74            19.2   6 167.6 123 3.92  1  0
#> 22.34            17.8   6 167.6 123 3.92  1  0
#> 21.47            16.4   8 275.8 180 3.07  0  0
#> 21.33            17.3   8 275.8 180 3.07  0  0
#> 21.78            15.2   8 275.8 180 3.07  0  0
#> 21.9175          10.4   8 472.0 205 2.93  0  0
#> 21.888           10.4   8 460.0 215 3.00  0  0
#> 21.42875         14.7   8 440.0 230 3.23  0  0
#> 28.27            32.4   4  78.7  66 4.08  1  1
#> 21.75            30.4   4  75.7  52 4.93  1  1
#> 27.24            33.9   4  71.1  65 4.22  1  1
#> 27.405           21.5   4 120.1  97 3.70  1  0
#> 22.15            15.5   8 318.0 150 2.76  0  0
#> 22.4525          15.2   8 304.0 150 3.15  0  0
#> 18.29            13.3   8 350.0 245 3.73  0  0
#> 22.8175          19.2   8 400.0 175 3.08  0  0
#> 26.64            27.3   4  79.0  66 4.08  1  1
#> 22.05            26.0   4 120.3  91 4.43  0  1
#> 20.6825          30.4   4  95.1 113 3.77  1  1
#> 18.4625          15.8   8 351.0 264 4.22  0  1
#> 17.8083333333333 19.7   6 145.0 175 3.62  0  1
#> 16.83125         15.0   8 301.0 335 3.54  0  1
#> 24.16            21.4   4 121.0 109 4.11  1  1