set_tidy_names() ensures its input has non-missing and unique names (duplicated names get a suffix of the format ..# where # is the position in the vector). Valid names are left unchanged, with the exception that existing suffixes are reorganized. tidy_names() is the workhorse behind set_tidy_names(), it treats the argument as a string to be used to name a data frame or a vector. repair_names() is an older version with different renaming heuristics, kept for backward compatibility. New code should prefer tidy_names().

set_tidy_names(x, syntactic = FALSE, quiet = FALSE)

tidy_names(name, syntactic = FALSE, quiet = FALSE)

repair_names(x, prefix = "V", sep = "")

Arguments

x

A named vector.

syntactic

Should all names be made syntactically valid via make.names()?

quiet

If TRUE suppresses output from this function

name

A character vector representing names.

prefix

A string, the prefix to use for new column names.

sep

A string inserted between the column name and de-duplicating number.

Value

x with valid names.

Examples

# Works for lists and vectors, too: set_tidy_names(3:5)
#> New names: #> -> ..1 #> -> ..2 #> -> ..3
#> ..1 ..2 ..3 #> 3 4 5
set_tidy_names(list(3, 4, 5))
#> New names: #> -> ..1 #> -> ..2 #> -> ..3
#> $..1 #> [1] 3 #> #> $..2 #> [1] 4 #> #> $..3 #> [1] 5 #>
# Clean data frames are left unchanged: set_tidy_names(mtcars)
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> [ reached getOption("max.print") -- omitted 23 rows ]
# By default, all rename operations are printed to the console: tbl <- as_tibble(structure(list(3, 4, 5), class = "data.frame"), validate = FALSE) set_tidy_names(tbl)
#> New names: #> -> ..1 #> -> ..2 #> -> ..3
#> # A tibble: 0 x 3 #> # ... with 3 variables: ..1 <dbl>, ..2 <dbl>, ..3 <dbl>
# Optionally, names can be made syntactic: tidy_names("a b", syntactic = TRUE)
#> New names: #> a b -> a.b
#> [1] "a.b"