This is a convenience function that sets the names of an object and returns it including the new names. It is most useful at the end of a function definition where one is creating the object to be returned and would prefer not to store it under a name just that the names can be assigned. In addition to the function setNames in base R the user can decide, whether rownames, colnames or simply the names are to be set. Names are recyled.

SetNames(x, ...)

Arguments

x

an object for which a names attribute will be meaningful

...

the names to be assigned to the object. This should be a character vector of names named dimnames, rownames, colnames or names. Setting rownames=NULL would remove existing rownames. All kind of names can be changed at the same time. Default would be names. Abbreviations are supported.

Value

An object of the same sort as object with the new names assigned.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

SetNames(1:5, names=letters[1:5])
#> a b c d e 
#> 1 2 3 4 5 

# the default, if no argument names are provided, is "names"
SetNames(1:5, letters[1:5])
#> a b c d e 
#> 1 2 3 4 5 

tab <- table(d.pizza$driver, d.pizza$wine_delivered)
# rownames and columnnames can be set at the same time
SetNames(BinomCI(tab[,1], rowSums(tab)), 
         rownames=rownames(tab), colnames=c("perc", "lci", "uci"))
#>                perc       lci       uci
#> Butcher   0.9042553 0.8279588 0.9488080
#> Carpenter 0.7925926 0.7402915 0.8366847
#> Carter    0.9177489 0.8751179 0.9467131
#> Farmer    0.8547009 0.7795898 0.9072606
#> Hunter    0.9025974 0.8455167 0.9400818
#> Miller    0.8720000 0.8021644 0.9196530
#> Taylor    0.8557214 0.8004723 0.8976285
         
# can also be used to set the names to an empty string
SetNames(diag(6), rownames="", colnames="")
#>             
#>  1 0 0 0 0 0
#>  0 1 0 0 0 0
#>  0 0 1 0 0 0
#>  0 0 0 1 0 0
#>  0 0 0 0 1 0
#>  0 0 0 0 0 1

# setting dimnames works as well
tab <- SetNames(
  as.table(rbind(c(84,43), c(10,92))), 
    dimnames= list(
       dipstick=c("positive","negative"),
       culture=c("positive","negative")))