R中列值相同的行如何排序

kcugc4gi  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(124)

我的df看起来像

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")))

我想排序或排序此数据的方式,如果列值是共同的两个或三个列,它应该在同一行。
就像我的输出应该我

F_d  M_d  Unique
 TP53 TP53 NA
  NA  CDKN1A CDKN1A
mwkjh3gx

mwkjh3gx1#

通过创建包含所有可能出现的列,可以解决问题

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 by column)df 2应该看起来像您所需要的那样!
希望这对你有帮助

相关问题