【问题标题】:Find IDs by comparing or currect subsetting of two data frames通过比较两个数据帧的子集或当前子集来查找 ID
【发布时间】:2017-11-01 21:29:41
【问题描述】:

我有两个数据框如下:

> dput(df1)
structure(list(Freq = c(1L, 1L, 1L, 1L, 0L, 0L, 2L, 1L), x = c(5L, 
2L, 8L, 6L, 3L, 4L, 8L, 6L), y = c(1L, 4L, 2L, 3L, 5L, 6L, 8L, 
8L), ID = 44:51), .Names = c("Freq", "x", "y", "ID"), class = "data.frame",row.names = c(NA, 
-8L))
> str(df1)
'data.frame':   8 obs. of  4 variables:
 $ Freq: int  1 1 1 1 0 0 2 1
 $ x   : int  5 2 8 6 3 4 8 6
 $ y   : int  1 4 2 3 5 6 8 8
 $ ID  : int  44 45 46 47 48 49 50 51

第二个:

 > dput(df2)
 structure(list(Freq = c(1L, 1L, 1L, 1L), x = c(5L, 2L, 8L, 6L
 ), y = c(1L, 4L, 2L, 3L)), .Names = c("Freq", "x", "y"), class = "data.frame", row.names = c(NA, 
-4L))
 > str(df2)
'data.frame':   4 obs. of  3 variables:
 $ Freq: int  1 1 1 1
 $ x   : int  5 2 8 6
 $ y   : int  1 4 2 3

我想从 df1 中找到 df2 的 ID,所以我想要的输出是:

 44 45 46 47

【问题讨论】:

    标签: r dataframe subset rows


    【解决方案1】:

    使用base::merge()

    的解决方案
    # Using OPs data
    merge(df1, df2)$ID
    # [1] 45 44 47 46
    

    这里merge 采用两个数据帧并通过共享列名(即df1 的子集df2)合并它们。

    【讨论】:

    • 谢谢!这就是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多