【发布时间】:2022-06-14 00:26:39
【问题描述】:
我有一个包含基因及其相关颜色的字符向量:
gene_colors<-c("protein_coding"="#1F78B4", "lncRNA"="#de08a0")
我正在尝试浏览另一个基因列表,如果该基因不在向量中,则添加随机颜色的基因:
library(tidyverse)
library(randomcoloR)
for(gene in other_genes){
if(!(gene %in% names(gene_colors))){
temp<-paste0(gene, '=', randomColor(1))
}
}
这是other_genes 中的内容:
[1] "IG_C_gene" "IG_C_pseudogene"
[3] "IG_J_gene" "IG_V_gene"
[5] "IG_V_pseudogene" "lncRNA"
[7] "miRNA" "misc_RNA"
[9] "Mt_rRNA" "polymorphic_pseudogene"
[11] "processed_pseudogene" "protein_coding"
如您所见,我尝试使用paste0(),而我之前尝试使用str_c(),但这两个都给了我一个类似"IG_C_gene=#ffd4bf"的字符串。我想在热图函数中使用gene_colors 向量,因此我需要将等号分开(即不在引号内,就像它是字符串中的字符一样),就像gene_colors 中的条目一样。有没有办法做到这一点?
【问题讨论】:
-
什么是
other_genes -
提示:如果您想匹配事物并且发现自己在循环,请停止,您可能只想使用
merge,这样效率更高。