根据另一个数据框中的列重新排序数据框中的行,但具有非唯一值
Reorder rows in a dataframe based on column in another dataframe but with non-unique values
我有 2 个数据框。
第一个数据框 df1 有 1246 行,如下所示:
gene1 gene2 gene3
AAAB.P1 1.23 2.28 -2.85
AABC.P1 4.06 -0.59 -2.42
ABCD.P1 3.26 2.19 -3.01
ABCD.R1 1.23 0.15 -2.30
DBCA.P1 1.67 -0.51 -3.24
第二个数据框 df2 也有 1246 行,如下所示:
id primary_diagnosis
1 ABCD carcinoma
2 ABCD carcinoma
3 AAAB AS
4 DBCA carcinoma
5 EFGH other
6 LMNO AS
我需要根据 id 列重新排列第二个 df,使其与第一个 df 的顺序相同。我尝试使用
df2<-df2[order(match(df2$id, substr(rownames(df1), 1, 4))), ]
但它只给了我 21 行的正确位置。我认为问题在于 df2 包含一些重复项。例如,ABCD 在 df1 中同时具有 ABCD.P1 和 ABCD.R1,但在 df2 中只有一个条目。他们进入哪个顺序并不重要,因为 P1 和 R1 都与癌相匹配。如何重新排序 df2 以匹配 df1 已有的任何顺序?
这个怎么样:
df1 <- read.table(text = "id gene1 gene2 gene3
AAAB.P1 1.23 2.28 -2.85
AABC.P1 4.06 -0.59 -2.42
ABCD.P1 3.26 2.19 -3.01
ABCD.R1 1.23 0.15 -2.30
DBCA.P1 1.67 -0.51 -3.24", sep=" ", header=TRUE)
rownames(df1) <- df1$id
df1 <- df1[,-1]
df2 <- read.table(text="id primary_diagnosis
ABCD carcinoma
ABCD carcinoma
AAAB AS
DBCA carcinoma
EFGH other
LMNO AS", sep=" ", header=TRUE)
df2[match(substr(rownames(df1), 1, 4), df2$id), ]
#> id primary_diagnosis
#> 3 AAAB AS
#> NA <NA> <NA>
#> 1 ABCD carcinoma
#> 1.1 ABCD carcinoma
#> 4 DBCA carcinoma
由 reprex package (v2.0.1)
创建于 2022-05-23
我们可以这样使用简单的arrange
:
library(dplyr)
df2 %>%
arrange(id, df1$id)
id primary_diagnosis
3 AAAB AS
1 ABCD carcinoma
2 ABCD carcinoma
4 DBCA carcinoma
5 EFGH other
6 LMNO AS
#df1
df1 <- structure(list(gene1 = c(1.23, 4.06, 3.26, 1.23, 1.67), gene2 = c(2.28,
-0.59, 2.19, 0.15, -0.51), gene3 = c(-2.85, -2.42, -3.01, -2.3,
-3.24)), class = "data.frame", row.names = c("AAAB.P1", "AABC.P1",
"ABCD.P1", "ABCD.R1", "DBCA.P1"))
#df2
df2 <- structure(list(id = c("ABCD", "ABCD", "AAAB", "DBCA", "EFGH",
"LMNO"), primary_diagnosis = c("carcinoma", "carcinoma", "AS",
"carcinoma", "other", "AS")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
您可以使用 df2[rank(rownames(df1), ties.method = "random"), ]
.
ties.method = "random"
参数确保在出现并列时每个排名都是唯一的。
我有 2 个数据框。
第一个数据框 df1 有 1246 行,如下所示:
gene1 gene2 gene3
AAAB.P1 1.23 2.28 -2.85
AABC.P1 4.06 -0.59 -2.42
ABCD.P1 3.26 2.19 -3.01
ABCD.R1 1.23 0.15 -2.30
DBCA.P1 1.67 -0.51 -3.24
第二个数据框 df2 也有 1246 行,如下所示:
id primary_diagnosis
1 ABCD carcinoma
2 ABCD carcinoma
3 AAAB AS
4 DBCA carcinoma
5 EFGH other
6 LMNO AS
我需要根据 id 列重新排列第二个 df,使其与第一个 df 的顺序相同。我尝试使用
df2<-df2[order(match(df2$id, substr(rownames(df1), 1, 4))), ]
但它只给了我 21 行的正确位置。我认为问题在于 df2 包含一些重复项。例如,ABCD 在 df1 中同时具有 ABCD.P1 和 ABCD.R1,但在 df2 中只有一个条目。他们进入哪个顺序并不重要,因为 P1 和 R1 都与癌相匹配。如何重新排序 df2 以匹配 df1 已有的任何顺序?
这个怎么样:
df1 <- read.table(text = "id gene1 gene2 gene3
AAAB.P1 1.23 2.28 -2.85
AABC.P1 4.06 -0.59 -2.42
ABCD.P1 3.26 2.19 -3.01
ABCD.R1 1.23 0.15 -2.30
DBCA.P1 1.67 -0.51 -3.24", sep=" ", header=TRUE)
rownames(df1) <- df1$id
df1 <- df1[,-1]
df2 <- read.table(text="id primary_diagnosis
ABCD carcinoma
ABCD carcinoma
AAAB AS
DBCA carcinoma
EFGH other
LMNO AS", sep=" ", header=TRUE)
df2[match(substr(rownames(df1), 1, 4), df2$id), ]
#> id primary_diagnosis
#> 3 AAAB AS
#> NA <NA> <NA>
#> 1 ABCD carcinoma
#> 1.1 ABCD carcinoma
#> 4 DBCA carcinoma
由 reprex package (v2.0.1)
创建于 2022-05-23我们可以这样使用简单的arrange
:
library(dplyr)
df2 %>%
arrange(id, df1$id)
id primary_diagnosis
3 AAAB AS
1 ABCD carcinoma
2 ABCD carcinoma
4 DBCA carcinoma
5 EFGH other
6 LMNO AS
#df1
df1 <- structure(list(gene1 = c(1.23, 4.06, 3.26, 1.23, 1.67), gene2 = c(2.28,
-0.59, 2.19, 0.15, -0.51), gene3 = c(-2.85, -2.42, -3.01, -2.3,
-3.24)), class = "data.frame", row.names = c("AAAB.P1", "AABC.P1",
"ABCD.P1", "ABCD.R1", "DBCA.P1"))
#df2
df2 <- structure(list(id = c("ABCD", "ABCD", "AAAB", "DBCA", "EFGH",
"LMNO"), primary_diagnosis = c("carcinoma", "carcinoma", "AS",
"carcinoma", "other", "AS")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
您可以使用 df2[rank(rownames(df1), ties.method = "random"), ]
.
ties.method = "random"
参数确保在出现并列时每个排名都是唯一的。