我如何将列表转换为所需列表

时间:2019-05-28 12:07:18

标签: r list

我有两列数据,第一列基因ID和第二列表达值

如下所示

6876    -2.735085846
59  -2.559180326
72  -2.41504926
4638    -1.785835164

我想要类似

> str(geneList)
 Named num [1:12495] 4.57 4.51 4.42 4.14 3.88 ...
 - attr(*, "names")= chr [1:12495] "4312" "8318" "10874" "55143" ...

但是当我在做

X = as.character(t(read.table(text ="
6876    -2.735085846
59  -2.559180326
72  -2.41504926
4638    -1.785835164")))

我得到

> str(X)
 chr [1:1418] "6876" "-2.735085846" "59" "-2.559180326" "72" "-2.41504926" "4638" "-1.785835164" "10398" "-1.588650179" ...
>

我如何制作类似于geneList的东西?

1 个答案:

答案 0 :(得分:1)

使用setNames读取数据后,只需使用read.table

X <- read.table(text = "
6876    -2.735085846
59  -2.559180326
72  -2.41504926
4638    -1.785835164
")


geneList <- setNames(X[[2]], X[[1]])
#     6876        59        72      4638 
#-2.735086 -2.559180 -2.415049 -1.785835 

并匹配您的预期输出:

str(geneList)
# Named num [1:4] -2.74 -2.56 -2.42 -1.79
# - attr(*, "names")= chr [1:4] "6876" "59" "72" "4638"