The R Project SVN R

Rev

Rev 76240 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/make.unique.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
76240 maechler 3
% Copyright 1995-2019 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
24855 ripley 6
\name{make.unique}
56186 murdoch 7
\alias{make.unique}
24855 ripley 8
\title{Make Character Strings Unique}
9
\description{
10
  Makes the elements of a character vector unique by
11
  appending sequence numbers to duplicates.
12
}
13
\usage{
25360 ripley 14
make.unique(names, sep = ".")
24855 ripley 15
}
16
\arguments{
85065 smeyer 17
  \item{names}{a character vector.}
24855 ripley 18
  \item{sep}{a character string used to separate a duplicate name from
19
    its sequence number.}
20
}
21
\value{
45778 ripley 22
  A character vector of same length as \code{names} with duplicates
23
  changed, in the current locale's encoding.
24855 ripley 24
}
25
\details{
26
  The algorithm used by \code{make.unique} has the property that
27
  \code{make.unique(c(A, B)) == make.unique(c(make.unique(A), B))}.
28
 
29
  In other words, you can append one string at a time to a vector,
30
  making it unique each time, and get the same result as applying
31
  \code{make.unique} to all of the strings at once.
61433 ripley 32
 
24855 ripley 33
  If character vector \code{A} is already unique, then
34
  \code{make.unique(c(A, B))} preserves \code{A}.
35
}
45778 ripley 36
\author{Thomas P. Minka}
24855 ripley 37
\seealso{
38
  \code{\link{make.names}}
39
}
40
\examples{
41
make.unique(c("a", "a", "a"))
42
make.unique(c(make.unique(c("a", "a")), "a"))
43
 
44
make.unique(c("a", "a", "a.2", "a"))
45
make.unique(c(make.unique(c("a", "a")), "a.2", "a"))
46
 
76240 maechler 47
## Now show a bit where this is used :
48
trace(make.unique)
49
## Applied in data.frame() constructions:
50
(d1 <- data.frame(x = 1, x = 2, x = 3)) # direct
51
 d2 <- data.frame(data.frame(x = 1, x = 2), x = 3) # pairwise
52
stopifnot(identical(d1, d2),
53
          colnames(d1) == c("x", "x.1", "x.2"))
54
untrace(make.unique)
24855 ripley 55
}
56
\keyword{character}