如何在R中访问实际的内部因素查找哈希表 [英] How to access actual internal factor lookup hashtable in R

查看:72
本文介绍了如何在R中访问实际的内部因素查找哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的Stackoverflow社区,

Dear Stackoverflow community,

我到处都看过,但是找不到这个问题的答案.当您将字符串向量更改为因子向量时,我试图访问R使用的因子查找表.我不是试图将字符串转换为因子,而是要获取因子变量基础的查找表,并将其存储为哈希表以供其他地方使用.

I have looked everywhere but can't find the answer to this question. I am trying to access the factor lookup table that R uses when you change a string vector into a factor vector. I am not trying to convert a string to a factor but rather to get the lookup table underlying the factor variable and store it as a hash table for use elsewhere.

我遇到了这个问题,因为我想在一系列不同长度的向量上使用此因子查找表,以将它们从字符串转换为数字.

I encountered the problem because I want to use this factor lookup table on a list of different length vectors, to convert them from strings to numbers.

即,我有一个要转换为数字的项目集列表,但是列表中的每个项目集都有不同数量的项目.

i.e., I have a list of item sets that I want to convert to numeric, but each set in the list has a different number of items.

到目前为止,我已经将向量列表转换为向量

So far, I have converted the list of vectors into a vector

vec <- unlist(list)
vec <- factor(vec)

现在,我想使用必须位于vec之下的因子查找表在原始列表上进行查找,但是我似乎找不到它.

Now I want to do a lookup on the original list with the factor lookup table which must be underlying vec, but I can't seem to find it.

推荐答案

我认为您要么想要将因子的元素映射到因子水平的元素的索引,就可以:

I think you either want the indexes which map the elements of the factor to elements of the factor levels, as in:

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

indx <- (f)
attributes(indx) <- NULL
indx
#> [1] 1 2 3 2 1

或者您希望内部使用哈希表来创建factor变量.不幸的是,在创建因子的过程中创建的任何哈希表都将由内部函数uniquematch创建,因此您将无法访问这些函数创建的任何内容(返回值除外)当然).如果您想要一个哈希表,以便可以用它索引与现有因子相同级别的字符向量,只需创建一个哈希表,如下所示:

or you want the hash tables used internally to create the factor variable. Unfortunately, any hash tables created in the process of creating a factor, would be created by the functions unique and match which are internal functions, so you won't have access to anything those functions create (other than the return value of course). If you want a hash table so you can use it to index a character vector with the same levels as your existing factor, just create a hash table, as in:

library(hash)
.levels <- levels(f)
h <- hash(keys = .levels,values = seq_along(.levels))
newVec <- sample(.levels,10,replace=T)
newVec
#> [1] "a" "b" "a" "a" "a" "c" "c" "b" "c" "a"
values(h,keys = newVec)
#> a b a a a c c b c a 
#> 1 2 1 1 1 3 3 2 3 1 

这篇关于如何在R中访问实际的内部因素查找哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆