【问题标题】:subset after a match occurs匹配发生后的子集
【发布时间】:2012-11-12 16:59:07
【问题描述】:

我有一个这样的数据框(特别是 data.frame 包含 50 列):

  "G1"            "G2"  
  SEP11          ABCC1   
  0.1365         0.1858   
  214223_at      ADAM19     
  0.1305         0.131   
  COPS4          BIK 
  0.1271         0.1143
  ACE            ALG3
  0.1333         0.119
  EMP3           GGH
  0.1246         0.1214

和另一个这样的data.frame(特别是data.frame包含50列):

   "G1"           "G2"  
  0.1365         0.1858   
  0.1271         0.1143    
  0.1246         0.1214 

我想要以下输出:

  "G1"           "G2"  
 SEP11          ABCC1  
 0.1365         0.1858  
 COPS4          BIK     
 0.1271         0.1143   
 EMP3           GGH
 0.1246         0.1214 

谁能帮帮我?

基本上,在 R 找到 data.frame 1 中的“0.1365”和 data.frame2 中的“0.1365”之间的匹配后,它会从 data.frame1 中提取对应的名称,该名称与存在匹配的数字相关联,并且这个数字也是因为我想回答这个问题:data.frame1 中的哪个元素与该数字相关联?

【问题讨论】:

  • 嗨,Roland,对不起,但由于我做了很多编辑,我认为这很混乱。
  • 不清楚是只有第一列的值需要匹配还是一行中的所有值都需要匹配。

标签: r


【解决方案1】:
df1 <- read.table(text=" G1            G2  
  SEP11          ABCC1   
  0.1365         0.1858   
  214223_at      ADAM19     
  0.1305         0.131   
  COPS4          BIK 
  0.1271         0.1143
  ACE            ALG3
  0.1333         0.119
  EMP3           GGH
  0.1246         0.1214",header=TRUE,stringsAsFactors=FALSE)

df2 <- read.table(text="G1           G2  
      0.1365         0.1858   
      0.1271         0.1143    
      0.1246         0.1214 
 ",header=TRUE,stringsAsFactors=FALSE)

#separate names and numbers
df1a <- df1[seq(from=1,to=nrow(df1)-1,by=2),]
df1b <- df1[seq(from=2,to=nrow(df1),by=2),]

#look up and merge again
df <- rbind(df1b[apply(df1b,1,paste,collapse=",") %in% apply(df2,1,paste,collapse=","),],
            df1a[apply(df1b,1,paste,collapse=",") %in% apply(df2,1,paste,collapse=","),])
df <- df[order(as.numeric(rownames(df))),]
#       G1     G2
#1   SEP11  ABCC1
#2  0.1365 0.1858
#5   COPS4    BIK
#6  0.1271 0.1143
#9    EMP3    GGH
#10 0.1246 0.1214

【讨论】:

    【解决方案2】:

    假设您的数据是成对的,这应该可以工作:

    1. 您的数据:

      df1 <- read.table(header = TRUE, text = '  "G1"            "G2"
                        SEP11          ABCC1
                        0.1365         0.1858
                        214223_at      ADAM19
                        0.1305         0.131
                        COPS4          BIK
                        0.1271         0.1143
                        ACE            ALG3
                        0.1333         0.119
                        EMP3           GGH
                        0.1246         0.1214')
      df2 <- read.table(header = TRUE, text = ' "G1"           "G2"
                        0.1365         0.1858
                        0.1271         0.1143
                        0.1246         0.1214 ')
      
    2. 匹配指定数据和上一行的数据

      myMatch <- which(df1$G1 %in% df2$G1)
      myMatch <- sort(c(myMatch, myMatch-1))
      
    3. 子集。

      df1[myMatch, ]
      #        G1     G2
      # 1   SEP11  ABCC1
      # 2  0.1365 0.1858
      # 5   COPS4    BIK
      # 6  0.1271 0.1143
      # 9    EMP3    GGH
      # 10 0.1246 0.1214
      

    更新

    借用一点 Roland 的方法,如果您尝试跨多个列进行匹配,那么确实merge 可能是更合适的方法。不幸的是,您的数据目前不是一种易于合并的形式,但这也很容易修复:

    1. 通过分离名称和值并cbind输出来“修复”您的“df1”data.frame

      df1.new <- cbind(df1[seq(from = 1, to = nrow(df1), by = 2), ], 
                       df1[seq(from = 2, to = nrow(df1), by = 2), ])
      
    2. 重命名数据前半部分的列以表明它们是名称。后半部分数据的列将被保留以进行合并。

      names(df1.new)[1:(ncol(df1.new)/2)] <- 
        paste(names(df1.new[1:(ncol(df1.new)/2)]), "Name", sep = ".")
      df1.new
      #     G1.Name G2.Name     G1     G2
      # 1     SEP11   ABCC1 0.1365 0.1858
      # 3 214223_at  ADAM19 0.1305  0.131
      # 5     COPS4     BIK 0.1271 0.1143
      # 7       ACE    ALG3 0.1333  0.119
      # 9      EMP3     GGH 0.1246 0.1214
      
    3. 使用merge() 获取您的数据“子集”。

      merge(df1.new, df2)
      #       G1     G2 G1.Name G2.Name
      # 1 0.1246 0.1214    EMP3     GGH
      # 2 0.1271 0.1143   COPS4     BIK
      # 3 0.1365 0.1858   SEP11   ABCC1
      

    一般来说,这个“更宽”的data.frame 可能更方便您使用。

    【讨论】:

    • 感谢您对 mrdwab 的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2013-01-25
    • 2021-10-14
    相关资源
    最近更新 更多