【问题标题】:selecting rows according to all covariates combinations of a different dataframe根据不同数据框的所有协变量组合选择行
【发布时间】:2013-01-21 14:43:57
【问题描述】:

我目前正试图弄清楚如何选择一个长数据帧 (long) 中的所有行,这些行呈现相同的 x1x2 组合来表征另一个数据帧 (short)。

简化数据为:

long <- read.table(text = "
  id_type   x1   x2

   1       0     0  
   1       0     1
   1       1     0
   1       1     1
   2       0     0
   2       0     1
   2       1     0
   2       1     1
   3       0     0  
   3       0     1
   3       1     0
   3       1     1
   4       0     0  
   4       0     1
   4       1     0
   4       1     1", 
header=TRUE) 

short <- read.table(text = "
   x1   x2    

   0     0    
   0     1", 
     header=TRUE) 

预期的输出是:

 id_type  x1    x2

   1       0     0  
   1       0     1
   2       0     0
   2       0     1
   3       0     0  
   3       0     1
   4       0     0  
   4       0     1

我试过用:

out <- long[unique(long[,c("x1", "x2")]) %in% unique(short[,c("x1", "x2")]), ] 

但是%in%收养在这里用错了..非常感谢您的帮助!

【问题讨论】:

    标签: r subset


    【解决方案1】:

    您正在请求内部连接:

    > merge(long, short)
      x1 x2 id_type
    1  0  0       1
    2  0  0       2
    3  0  0       3
    4  0  0       4
    5  0  1       1
    6  0  1       2
    7  0  1       3
    8  0  1       4
    

    【讨论】:

    • 有趣的是,我想知道 OP 之前的相关问题是否是他们一开始就想要的!
    猜你喜欢
    • 2018-09-07
    • 2011-04-18
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多