如何在R中的4个数据帧之间只保留公共行名称?

如何在R中的4个数据帧之间只保留公共行名称?,r,matrix,R,Matrix,我有4个基因数据框,每个数据框都有基因名称作为行和大约20列样本数据。因此,每个矩阵都有相同数量的行: A:10000个基因 B:15000个基因 C:35000个基因 D:12000个基因 这是我尝试的,它没有选择9000个常见行基因的完整列表 Data_A = read.csv("matrix_A.csv"); Data_B = read.csv("matrix_B.csv"); Data_C = read.csv("matrix_C.csv"); Data_D = read.csv("ma

我有4个基因数据框,每个数据框都有基因名称作为行和大约20列样本数据。因此,每个矩阵都有相同数量的行:

A:10000个基因 B:15000个基因 C:35000个基因 D:12000个基因 这是我尝试的,它没有选择9000个常见行基因的完整列表

Data_A = read.csv("matrix_A.csv");
Data_B = read.csv("matrix_B.csv");
Data_C = read.csv("matrix_C.csv");
Data_D = read.csv("matrix_D.csv");

Expr_A = as.data.frame(t(Data_A[, -c(1:8)]))
Expr_B = as.data.frame(t(Data_B[, -c(1:8)]))
Expr_C = as.data.frame(t(Data_C[, -c(1:8)]))
Expr_D = as.data.frame(t(Data_D[, -c(1:8)]))

commonGenes1 = intersect (rownames(Data_A),rownames(Data_D))
commonGenes2 = intersect (rownames(Data_B),rownames(Data_D))
commonGenes3 = intersect (rownames(Data_C),rownames(Data_D))

Data_A = Data_A[commonGenes1,]
Data_B = Data_B[commonGenes2,]
Data_C = Data_C[commonGenes3,]
它们都有9000个共同的基因,尽管数据太大了,我无法在Excel中实现这一点。我用R来处理数据,有没有办法在R中的4个数据帧之间选择共同的基因

以下是4个矩阵的示例:

按照你的标题,让我们把事情列在一个列表中,这是一个很好的做法

list_of_data = list(Data_A, Data_B, Data_C, Data_D)
## for demo purposes, you can use
# list_of_data = list(mtcars[1:6, ], mtcars[4:9, ])

# this will get the intersection of the row.names for everything in the list
common_names = Reduce(intersect, lapply(list_of_data, row.names))

list_of_data = lapply(list_of_data, function(x) { x[row.names(x) %in% common_names,] })

感谢@eipi10提供了一种更好的方法来过滤列表中每个数据帧的行。查看lame for循环的修订历史记录。

按照标题所示,让我们实际将内容放入一个列表中,这是一个很好的实践

list_of_data = list(Data_A, Data_B, Data_C, Data_D)
## for demo purposes, you can use
# list_of_data = list(mtcars[1:6, ], mtcars[4:9, ])

# this will get the intersection of the row.names for everything in the list
common_names = Reduce(intersect, lapply(list_of_data, row.names))

list_of_data = lapply(list_of_data, function(x) { x[row.names(x) %in% common_names,] })
感谢@eipi10提供了一种更好的方法来过滤列表中每个数据帧的行。查看lame for循环的修订历史记录。

这是怎么回事

# Create some fake data:
set.seed(123)
m1 <- cbind(sample(1:5), round(rnorm(5),2))
m2 <- cbind(sample(1:5), round(rnorm(5),2))
m3 <- cbind(sample(1:5), round(rnorm(5),2))
m4 <- cbind(sample(1:5), round(rnorm(5),2))
rownames(m1) <- LETTERS[sample(1:10, 5)]
rownames(m2) <- LETTERS[sample(1:10, 5)]
rownames(m3) <- LETTERS[sample(1:10, 5)]
rownames(m4) <- LETTERS[sample(1:10, 5)]


ind <- sapply(list(m1,m2,m3), function(x) intersect(rownames(x), rownames(m4)))
mapply(function(x, y) x[rownames(x) %in% y,], x = list(m1,m2,m3), y = ind)
[[1]]
  [,1]  [,2]
A    4  1.24
D    5 -0.11
E    1  0.18

[[2]]
  [,1]  [,2]
E    5  1.22
C    2 -0.56

[[3]]
  [,1]  [,2]
A    2 -0.22
C    1 -0.33
这个怎么样

# Create some fake data:
set.seed(123)
m1 <- cbind(sample(1:5), round(rnorm(5),2))
m2 <- cbind(sample(1:5), round(rnorm(5),2))
m3 <- cbind(sample(1:5), round(rnorm(5),2))
m4 <- cbind(sample(1:5), round(rnorm(5),2))
rownames(m1) <- LETTERS[sample(1:10, 5)]
rownames(m2) <- LETTERS[sample(1:10, 5)]
rownames(m3) <- LETTERS[sample(1:10, 5)]
rownames(m4) <- LETTERS[sample(1:10, 5)]


ind <- sapply(list(m1,m2,m3), function(x) intersect(rownames(x), rownames(m4)))
mapply(function(x, y) x[rownames(x) %in% y,], x = list(m1,m2,m3), y = ind)
[[1]]
  [,1]  [,2]
A    4  1.24
D    5 -0.11
E    1  0.18

[[2]]
  [,1]  [,2]
E    5  1.22
C    2 -0.56

[[3]]
  [,1]  [,2]
A    2 -0.22
C    1 -0.33


请给我一份复印件example@DatamineR当然,我只是用我尝试过的部分编辑了这篇文章,但没有效果。因为你没有提供数据,所以这篇文章实际上不可复制…@DatamineR我无法通过stackoverflow向你发送50000行数据。。每个矩阵在第1列有名称,第2列到第x列只是数字。。我们想要在4个矩阵之间有一个共同的名字列表。谢谢DatamineR的意思是,任何用户都应该能够运行您提供的代码,以便他们能够更快地帮助您。除非您希望提供指向矩阵A.csv、矩阵B.csv等不推荐的文件的链接,否则您应该能够从代码中构造示例数据。对于不太大的样本,建议使用dput。否则,提出一个模拟问题所需数据特征的函数。请给出一个可复制的example@DatamineR当然我只是用我尝试过的部分编辑了这篇文章,但没有效果。因为你没有提供数据,所以它实际上不可复制…@DatamineR我无法通过stackoverflow向你发送50000行数据。。每个矩阵在第1列有名称,第2列到第x列只是数字。。我们想要在4个矩阵之间有一个共同的名字列表。谢谢DatamineR的意思是,任何用户都应该能够运行您提供的代码,以便他们能够更快地帮助您。除非您希望提供指向矩阵A.csv、矩阵B.csv等不推荐的文件的链接,否则您应该能够从代码中构造示例数据。对于不太大的样本,建议使用dput。否则,请使用一个模拟问题所需的数据特性的函数。Gregor,在最后一步中,关于_数据的lapplylist_,functionx{x[row.namesx%in%common_names,]}谢谢,有没有办法查看_数据的相交列表_的长度?另外,我可以将相交的结果输出到文件中吗?当然可以。交集名称存储在公共名称中,因此您可以使用lengthcommon\u名称查看有多少个交集名称,您可以使用write.csvcommon\u名称,file=rows in common.csv写出列表。要检查结果数据帧中的行数,请使用lapplylist\u of_data,nrow.Gregor,最后一步,如何使用lapplylist\u of_data,functionx{x[row.namesx%在%common_names中,]}谢谢,有办法查看相交列表的长度吗?另外,我可以将相交结果输出到文件中吗?当然可以。相交名称存储在公共名称中,因此您可以使用lengthcommon名称查看有多少个,您可以使用write.csvcommon\u名称写出列表,file=common.csv中的行结果数据帧中行的r,数据的lapplylist,nrow。