gpt4 book ai didi

返回最高水平因子

转载 作者:行者123 更新时间:2023-12-04 09:32:31 26 4
gpt4 key购买 nike

我正在尝试使用有序的分类变量。似乎 max min 函数应该适用于有序类别,但事实并非如此。

var<-factor(c("1","6","4","3","5","2"),levels=c("1","6","4","3","5","2"))
max(levels(var))

我希望代码返回最后一个因子级别 (2),但它返回第二个 (6)。我究竟做错了什么?在此先感谢您的帮助

最佳答案

只需指定 ordered factor 中的参数功能,然后它将工作。请参阅以下内容:

#set the ordered argument to TRUE, so that R understands the order of the levels
#and i.e. which one is min and which is max
var<-factor(c("1","6","4","3","5","2"),levels=c("1","6","4","3","5","2"), ordered=TRUE)

#and then
> max(var)
[1] 2
Levels: 1 < 6 < 4 < 3 < 5 < 2

关于返回最高水平因子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com