Skip to content

Commit 06906fa

Browse files
author
smeyer
committed
tune factor(<integer|logical>)
git-svn-id: https://svn.r-project.org/R/trunk@88074 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 61813a0 commit 06906fa

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/library/base/R/factor.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/base/R/factor.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2022 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -21,13 +21,17 @@ factor <- function(x = character(), levels, labels = levels,
2121
{
2222
if(is.null(x)) x <- character()
2323
nx <- names(x)
24+
matchAsChar <- is.object(x) ||
25+
!(is.character(x) || is.integer(x) || is.logical(x))
2426
if (missing(levels)) {
2527
y <- unique(x, nmax = nmax)
2628
ind <- order(y)
27-
levels <- unique(as.character(y)[ind])
29+
if (matchAsChar)
30+
y <- as.character(y)
31+
levels <- unique(y[ind])
2832
}
2933
force(ordered) # check if original x is an ordered factor
30-
if(!is.character(x))
34+
if (matchAsChar)
3135
x <- as.character(x)
3236
## levels could be a long vector, but match will not handle that.
3337
levels <- levels[is.na(match(levels, exclude))]

0 commit comments

Comments
 (0)