繁体   English   中英

发生匹配后的子集

[英]subset after a match occurs

我有一个像这样的数据框(特别是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中的哪个元素与该数字相关联?

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

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

  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此“更宽”的data.frame可能更方便您使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM