x <- factor(letters)
x
# [1] a b c d e f g h i j k l m n o p q r s t u v w x y z
#Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
is.factor(x)
# TRUE
factor(x,labels = 1:length(levels(x)))
# [1] 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
ifelse(is.factor(x),factor(x,labels = 1:length(levels(x))),x)
# 1
y <- 26:1
ifelse(is.factor(y),factor(x,labels = 1:length(levels(y))),y)
# 26
为什么ifelse的每一部分都是正确的,但是函数返回的就不是想要的呢?