提取因子水平时出现意外行为
有人可以解释为什么 level() 显示三个因子级别,而您可以看到向量只有两个?
> str(walk.df)
'data.frame': 10 obs. of 4 variables:
$ walker : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2
> walk.df$walker
[1] 1 1 1 1 1 2 2 2 2 2
Levels: 1 2 3
我想提取一个级别向量,我认为这是正确的方法,但正如你所看到的,一个三潜入其中,这扰乱了我的功能。
> as.numeric(levels(walk.df$walker))
[1] 1 2 3
Can someone explain why levels() shows three factor levels, while you can see that the vector has only two?
> str(walk.df)
'data.frame': 10 obs. of 4 variables:
$ walker : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2
> walk.df$walker
[1] 1 1 1 1 1 2 2 2 2 2
Levels: 1 2 3
I would like to extract a vector of levels, and I thought this was the proper way, but as you can see, a three sneaks in there which is messing up my function.
> as.numeric(levels(walk.df$walker))
[1] 1 2 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能 walk.df 是具有 3 个级别的因子变量的子集。
比如说,
那么 b 有 3 个级别。
删除额外级别的一个简单方法是:
或者如果您无法访问原始变量,
probably walk.df is a subset of the factor variable with 3 levels.
say,
then b has 3 levels.
A easy way to drop extra level is:
or if you cannot access the original variable,
您可以将多个因子级别分配给包含两个级别的因子:
甚至根本没有级别:
You can assign several factor levels to a factor that contains two levels:
or even no levels at all: