简体   繁体   English

向量转换为列表的问题

[英]Issues with conversion of vectors into lists

I have read in two data matrices using read.table, and wish to find the intersection between the header list of M1 and a vector in M2. 我已经使用read.table读取了两个数据矩阵,并希望找到M1的标题列表和M2中的向量之间的交集。 However, if I use intersect(names(M1),M2[2,]), I get an error message because M2[2,] is a vector while M1 is a list. 但是,如果我使用intersect(names(M1),M2 [2,]),则会收到错误消息,因为M2 [2,]是向量,而M1是列表。 I thought that using as.list(M2[2,]) would correct this problem, but it does not. 我以为使用as.list(M2 [2,])可以解决此问题,但事实并非如此。 How can I convert the data structure of vector M2[2,] into a list so that its intersection with names(M1) would be a defined operation? 如何将向量M2 [2,]的数据结构转换为列表,以使其与names(M1)的交集成为已定义的操作?

(This is too long for a comment.) (此评论太长了。)

The following works for me: 以下对我有用:

set.seed(3)

M1 = data.frame(matrix(runif(100), ncol=5))
colnames(M1) = letters[1:5]

M2 = data.frame(matrix(rnorm(100), nrow=5))
M2[2,] = sample(letters, 20, replace=T)
intersect(names(M1),M2[2,])

Can you say more about your situation, and what is not working? 您能否再谈谈您的情况,什么不起作用? It may help to read this: how-to-make-a-great-r-reproducible-example . 阅读以下内容可能会有所帮助: 如何制作一个出色的r可复制示例

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

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