英文:
How to sort the Rows with identical column values in R
问题
我的数据框看起来像这样:
dput(Data)
structure(c(NA, "FGFR3", "FAT1", "ARID1A", "CREBBP", "HRAS",
"SF3B1", "RHOB", "FBXW7", "KRAS", "TP53", "PIK3CA", "RHOA", "ASXL2",
"HLA-A", "APC", "ATM", "ARID2", "PTEN", "CDM1", "RBM10", "ERBB4",
"ERCC2", "BAP1", "KMT2D", "ERBB2", "SMC1A", "RB1", "BCLAF1",
NA, NA, NA, NA, NA, NA, NA, "TP53", "RHOA", "FGFR3", "SF3B1",
"PTEN", "RB1", "FAT1", "KDM6A", "ARID1A", "PIK3CA", "CDKN1A",
"ERBB4", "RBM10", "ASXL2", "HRAS", "BAP1", "KMT2A", "ERBB3",
"RHOB", "KRAS", "APC", "KMT2C", "BCLAF1", "KMT2D", "CDKN2A",
"PSIP1", "FBXW7", "HLA-A", "ERBB2", "ATM", "RXRA", "CREBBP",
"EP300", "ARID2", "KDM6A", "CDKN1A", "KMT2A", "ERBB3", "KMT2C",
"CDKN2A", "PSIP1", "RXRA", "EP300", NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), dim = c(35L, 3L), dimnames = list(NULL, c("F_d", "M_d", "unique")))
我想按照这样的方式对这些数据进行排序或排序,如果某一列的值在两列或三列之间是相同的,它应该在同一行中。
我的输出应该是:
F_d M_d Unique
1 TP53 TP53 NA
2 NA CDKN1A CDKN1A
英文:
My df looks like
dput(Data)
structure(c(NA, "FGFR3", "FAT1", "ARID1A", "CREBBP", "HRAS",
"SF3B1", "RHOB", "FBXW7", "KRAS", "TP53", "PIK3CA", "RHOA", "ASXL2",
"HLA-A", "APC", "ATM", "ARID2", "PTEN", "CDH1", "RBM10", "ERBB4",
"ERCC2", "BAP1", "KMT2D", "ERBB2", "SMC1A", "RB1", "BCLAF1",
NA, NA, NA, NA, NA, NA, NA, "TP53", "RHOA", "FGFR3", "SF3B1",
"PTEN", "RB1", "FAT1", "KDM6A", "ARID1A", "PIK3CA", "CDKN1A",
"ERBB4", "RBM10", "ASXL2", "HRAS", "BAP1", "KMT2A", "ERBB3",
"RHOB", "KRAS", "APC", "KMT2C", "BCLAF1", "KMT2D", "CDKN2A",
"PSIP1", "FBXW7", "HLA-A", "ERBB2", "ATM", "RXRA", "CREBBP",
"EP300", "ARID2", "KDM6A", "CDKN1A", "KMT2A", "ERBB3", "KMT2C",
"CDKN2A", "PSIP1", "RXRA", "EP300", NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), dim = c(35L, 3L), dimnames = list(NULL, c("F_d",
"M_d", "unique")))
I want to sort or order this data in a way that if a column value is common between two or three column it should be in same row.
Like my output should me
F_d M_d Unique
TP53 TP53 NA
NA CDKN1A CDKN1A
答案1
得分: 1
通过创建一个包含所有可能出现的列,你可以解决你的问题。
library(dplyr)
df = as.data.frame(df)
list_tot = data.frame(x=unlist(df), row.names = 1:105) %>%
distinct() %>%
filter(is.na(x)==FALSE)
interm1 <- left_join(list_tot,
df %>% select(F_d) %>% mutate(x=F_d),
by="x")
interm2 <- left_join(interm1,
df %>% select(M_d) %>% mutate(x=M_d),
by="x")
df2 <- left_join(interm2,
df %>% select(unique) %>% mutate(x=unique),
by="x") %>%
select(-x)
解释这段代码,list_tot 将基于表中的所有项创建一个唯一的列表。然后通过 left_join 查看这些项是否在不同的列中(每个列都需要一个 left_join)。df2 应该看起来像你需要的样子!
希望这对你有帮助!
英文:
By creating a column with all the possible occurences, you can solve your problem
library(dplyr)
df = as.data.frame(df)
list_tot = data.frame(x=unlist(df), row.names = 1:105) %>%
distinct() %>%
filter(is.na(x)==FALSE)
interm1 <- left_join(list_tot,
df %>% select(F_d) %>% mutate(x=F_d),
by="x")
interm2 <- left_join(interm1,
df %>% select(M_d) %>% mutate(x=M_d),
by="x")
df2 <- left_join(interm2,
df %>% select(unique) %>% mutate(x=unique),
by="x") %>% select(-x)
To explain the code, list_tot will create a unique list based on all the items in your table. Then the left_join to see if those items are in the different columns (one left_join by column needed)
df2 should look like what you need!
Hope this will help you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论