【问题标题】:Remove row if certain variable is the same as the row above [duplicate]如果某些变量与上面的行相同,则删除行[重复]
【发布时间】:2015-10-01 14:40:20
【问题描述】:

我会以这个问题为基础,类似于Remove Rows From Data Frame where a Row match a String

例如:

A,B,org.id
4,3,Foo
2,3,Bar
2,4,Bar
7,5,Zap
7,4,Zap
7,3,Zap

我将如何返回一个排除所有 org.id 与上述行相同的行的数据框?

A,B,org.id
4,3,Foo
2,3,Bar
7,5,Zap

猜测:也许 melt() 或 cast() 函数可以解决问题。 (我只知道如何在 excel 中执行此操作,我必须在其中创建一个新数据框并执行 IF[a2=a1,0,a2]。)

这个问题也类似于Subtract the previous row of data where the id is the same as the row above,但那是在sql中。

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    你可以试试duplicated

     df1[!duplicated(df1$org.id),]
     #   A B org.id
     #1 4 3    Foo
     #2 2 3    Bar
     #4 7 5    Zap
    

    或者使用unique 和by 选项

     library(data.table)
     unique(setDT(df1), by='org.id')
    

    【讨论】:

    • 我试过 head((dff_all2[duplicated(dff_all2$id), c(1,4,6,17)])) # 但它输出:country_id id device_model 177 BR 776962 iPhone5,1 (iPhone 5) Organic 189 BR 687574 GT-I9505 samsung SEM 310 BR 1037119 XT1058 motorola Organic 439 BR 735454 GT-I8190L samsung Organic 805 BR 779030 GT-S5360B samsung 4 GSM) 直接
    • @jacob 我的代码基于您展示的示例。请使用导致异常的新示例(最好使用dput)更新您的帖子。
    猜你喜欢
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2020-10-10
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多