简体   繁体   English

将二维矩阵转换为向量并在R中进行索引

[英]2D matrix into vector and index in R

I have a matrix as follows: 我有一个如下的矩阵:

> td
  doc_id        d_name
1      1    John Smith
2      3  Casey Newton
3      5 Ricardo Adams
4      6  Jonah Jensen

I want to convert into a name vector which is indexed by its id: 我想转换为通过其ID索引的名称向量:

> df
John Smith  Casey Newton Ricardo Adams  Jonah Jensen 
        1             3             5             6 

Is there a way this is possible? 有没有办法做到这一点? Or may be a more efficient approach? 还是更有效的方法?

Yes. 是。 The simplest solution is: 最简单的解决方案是:

a= td[,1]
names(a) = td[,2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM