将 tibble 转换为 R 中的命名向量

Turn a tibble into a named vector in R

我想将 R 中的一个 tibble(它的一部分如下所示)变成一个命名向量,其中每个唯一的 gs_name 映射到与其对应的 ensembl_gene 列表.我该怎么做?

# A tibble: 6 x 2
  gs_name                  ensembl_gene  
  <chr>                    <chr>         
1 ABBUD_LIF_SIGNALING_1_DN WBGene00002717
2 ABBUD_LIF_SIGNALING_1_DN WBGene00006518
3 ABBUD_LIF_SIGNALING_1_DN WBGene00000542
4 ABBUD_UP WBGene00000546
5 ABBUD_UP WBGene00013434
6 ABBUD_UP WBGene00001579

您可以使用 split 获取命名列表。

res <- split(df$ensembl_genes, df$gs_name)

deframe() 在这里也很有用

library(tidyverse)

df <- read_csv("gs_name,ensembl_gene  
ABBUD_LIF_SIGNALING_1_DN,WBGene00002717
ABBUD_LIF_SIGNALING_1_DN,WBGene00006518
ABBUD_LIF_SIGNALING_1_DN,WBGene00000542
ABBUD_UP,WBGene00000546
ABBUD_UP,WBGene00013434
ABBUD_UP,WBGene00001579")

deframe(df)
#> ABBUD_LIF_SIGNALING_1_DN ABBUD_LIF_SIGNALING_1_DN ABBUD_LIF_SIGNALING_1_DN 
#>         "WBGene00002717"         "WBGene00006518"         "WBGene00000542" 
#>                 ABBUD_UP                 ABBUD_UP                 ABBUD_UP 
#>         "WBGene00000546"         "WBGene00013434"         "WBGene00001579"

reprex package (v1.0.0)

于 2021-07-14 创建