简体   繁体   English

向量因子水平的指标

[英]indices of factor level of vector

How to get vector containing indices of each element in the level attribute of the vector k? 如何获得包含向量k的level属性中每个元素的索引的向量? That is how to get vector (1, 2, 1) for the k ? 那就是如何获得k的向量(1、2、1)?

 k <- factor(c('a', 'b', 'a'))
> k
[1] a b a
Levels: a b

We can use match 我们可以使用match

match(k, levels(k))
#[1] 1 2 1

Or convert to numeric to get the integer storage values 或转换为数字以获得整数存储值

as.integer(k)
[1] 1 2 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM