最好/更快?在R中反转多对多关系列表的方法



什么是比下面更好的方法,即更快地反转R中的多对多关系。这个例子并不重要,但对于这样大小的列表,初始列表约36000个值,反向列表约11500个值,执行此操作所需的时间。是否可以矢量化或使用mapply?

library(hgu95av2.db)
xx <- as.list(hgu95av2ALIAS2PROBE)
## aliases that have probes
alias_values <- xx[!is.na(xx)]
## reverse the many to many relationship
probe_to_alias = list()
for (alias in names(alias_values)) {
for (probe_id in alias_values[[alias]]) {
if (!(probe_id %in% names(probe_to_alias))) {
probe_to_alias[[probe_id]] <- c(alias)
} else {
probe_to_alias[[probe_id]] <- append(probe_to_alias[[probe_id]], alias)
}
}
}
probe_to_alias[1:2]

这里有一个改进:

library(hgu95av2.db)
xx <- as.list(hgu95av2ALIAS2PROBE)
## aliases that have probes
alias_values <- xx[!is.na(xx)]
## reverse the many to many relationship
probe_to_alias = list()
for (alias in names(alias_values)) {
for (probe_id in alias_values[[alias]]) {
probe_to_alias[[probe_id]] <- append(probe_to_alias[[probe_id]], alias)
}
}

不同的是,它不会检查探测器是否已经有一个条目。那张支票花了很多时间,尤其是在名单越来越长的时候。

我还用lapply尝试了很多不同的选择,但找不到更快的选择。

最新更新