代码之家  ›  专栏  ›  技术社区  ›  Banjo

按ID替换行

r
  •  2
  • Banjo  · 技术社区  · 8 年前

    我有两个数据帧 full :

    library(dplyr)
    
    full %>% glimpse()
    Observations: 2,919
    Variables: 10
    $ Id           <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15...
    $ BsmtQual     <fct> Gd, Gd, Gd, TA, Gd, Gd, Ex, Gd, TA, TA, TA, Ex, T...
    $ BsmtCond     <fct> TA, TA, TA, Gd, TA, TA, TA, TA, TA, TA, TA, TA, T...
    $ BsmtExposure <fct> No, Gd, Mn, No, Av, No, Av, Mn, No, No, No, No, N...
    $ BsmtFinType1 <fct> GLQ, ALQ, GLQ, ALQ, GLQ, GLQ, GLQ, ALQ, Unf, GLQ,...
    $ BsmtFinSF1   <int> 706, 978, 486, 216, 655, 732, 1369, 859, 0, 851, ...
    $ BsmtFinType2 <fct> Unf, Unf, Unf, Unf, Unf, Unf, Unf, BLQ, Unf, Unf,...
    $ BsmtFinSF2   <int> 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, ...
    $ BsmtUnfSF    <int> 150, 284, 434, 540, 490, 64, 317, 216, 952, 140, ...
    $ TotalBsmtSF  <int> 856, 1262, 920, 756, 1145, 796, 1686, 1107, 952, ...
    

    还有这个 dat1

    dat1 %>% glimpse()
    Observations: 88
    Variables: 10
    $ Id           <int> 18, 40, 91, 103, 157, 183, 260, 333, 343, 363, 37...
    $ BsmtQual     <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
    $ BsmtCond     <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
    $ BsmtExposure <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
    $ BsmtFinType1 <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
    $ BsmtFinSF1   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
    $ BsmtFinType2 <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
    $ BsmtFinSF2   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
    $ BsmtUnfSF    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
    $ TotalBsmtSF  <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
    

    我要替换中的行 满的 一排排的 dat1 通过匹配Id。我用 merge tidyverse 但它没有起作用。

    3 回复  |  直到 8 年前
        1
  •  1
  •   MKR    8 年前

    一种选择是使用 anti_join semi_join 然后使用 bind_rows .以下解决方案将提供所需的记录。

    library(dplyr)
    
    bind_rows(anti_join(full, dat1, by="ID"), semi_join(dat1, full, by="ID"))
    
        2
  •  0
  •   Satish Chilloji    8 年前

    在R中使用SQLDF库,并在full和data1之间的id上进行内部联接。从完整数据框中选择变量。

        3
  •  -1
  •   Thomas Guillerme    8 年前

    你可能需要 rbind cbind (如果没有运行示例,很难说):

    ## Combining by columns (what seems to be your case)
    combined <- cbind(full, dat1)
    ## Combining by rows
    combined <- rbind(full, dat1)