google了很多次结果不是我想要的:
下面提供了一个示例数据集:
year = c(1991,1996,2001,2006,2011,2016,2021)
factor(a,levels = c(1991,1996,2001,2011,2016,2021))
结果是:
[1] 1991 1996 2001 <NA> 2011 2016 2021
Levels: 1991 1996 2001 2011 2016 2021
我想将
2006
的水平设置为与2001
相同,因此,我的有利结果将是:
[1] 1991 1996 2001 2006 2011 2016 2021
Levels: 1991 1996 2001 2011 2016 2021
是否可以在不改变向量
2006
的原始内容的情况下将2001
的级别更改为与year
相同?
当你深挖
factor
的源码时,我想你心里就会有答案了(我觉得你的问题应该是“No”)
> factor
function (x = character(), levels, labels = levels, exclude = NA,
ordered = is.ordered(x), nmax = NA)
{
if (is.null(x))
x <- character()
nx <- names(x)
if (missing(levels)) {
y <- unique(x, nmax = nmax)
ind <- order(y)
levels <- unique(as.character(y)[ind])
}
force(ordered)
if (!is.character(x))
x <- as.character(x)
levels <- levels[is.na(match(levels, exclude))]
f <- match(x, levels)
if (!is.null(nx))
names(f) <- nx
if (missing(labels)) {
levels(f) <- as.character(levels)
}
else {
nlab <- length(labels)
if (nlab == length(levels)) {
nlevs <- unique(xlevs <- as.character(labels))
at <- attributes(f)
at$levels <- nlevs
f <- match(xlevs, nlevs)[f]
attributes(f) <- at
}
else if (nlab == 1L)
levels(f) <- paste0(labels, seq_along(levels))
else stop(gettextf("invalid 'labels'; length %d should be 1 or %d",
nlab, length(levels)), domain = NA)
}
class(f) <- c(if (ordered) "ordered", "factor")
f
}
<bytecode: 0x00000186f0fe3640>
<environment: namespace:base>
如我们所见,如果未提供
levels
参数,则 unique(x, nmax = nmax)
由 levels
生成,或者,levels[is.na(match(levels, exclude))]
与给定的 levels
。这意味着,对于两个 level
值,您不可能使用一个 x
。
这是不可能的,不知道你的目的是什么,但你也许可以做这样的事情。原始值将是名称。
year = c(1991,1996,2001,2006,2011,2016,2021,2006)
year2 <- factor(year,levels = c(1991,1996,2001,2006,2011,2016,2021), labels = c(1991,1996,2001,2001,2011,2016,2021))
names(year2) <- year
year2
1991 1996 2001 2006 2011 2016 2021 2006
1991 1996 2001 2001 2011 2016 2021 2001
Levels: 1991 1996 2001 2011 2016 2021
str(year2)
Factor w/ 6 levels "1991","1996",..: 1 2 3 3 4 5 6 3
- attr(*, "names")= chr [1:8] "1991" "1996" "2001" "2006" ...