Skip to contents

This function can be used to rename the columns a safeframe (that is, adjust variable names).

Usage

# S3 method for class 'safeframe'
names(x) <- value

Arguments

x

a safeframe object

value

a character vector to set the new names of the columns of x

Value

a safeframe with new column names

Examples

## create safeframe
x <- make_safeframe(cars,
  speed = "Miles per hour",
  dist = "Distance in miles"
)
head(x)
#> 
#> // safeframe object
#>   speed dist
#> 1     4    2
#> 2     4   10
#> 3     7    4
#> 4     7   22
#> 5     8   16
#> 6     9   10
#> 
#> labelled variables:
#>  speed - Miles per hour
#>  dist - Distance in miles 

## change names
names(x)[1] <- "mph"

## see results: columns have been updated
head(x)
#> 
#> // safeframe object
#>   mph dist
#> 1   4    2
#> 2   4   10
#> 3   7    4
#> 4   7   22
#> 5   8   16
#> 6   9   10
#> 
#> labelled variables:
#>  mph - Miles per hour
#>  dist - Distance in miles 

# This also works with using `dplyr::rename()` because it uses names<-()
# under the hood
if (require(dplyr) && require(magrittr)) {
  x <- x %>%
    rename(speed = "mph")
  head(x)
  labels(x)
}
#> $speed
#> [1] "Miles per hour"
#> 
#> $dist
#> [1] "Distance in miles"
#>