Delete all rows coresponding to given ID

前端 未结 3 892
别跟我提以往
别跟我提以往 2021-01-27 06:21

Data overview:

> str(dataStart[c(\"gvkey\",\"DEF\",\"FittedRob\",\"NewCol\")])
\'data.frame\':   1000 obs. of  4 variables:
$ gvkey         : int  1004 1004          


        
相关标签:
3条回答
  • 2021-01-27 07:02

    You first have to select all gvkey's you want to delete:

    keys_to_delete <- unique(NewDataFrame$gvkey[NewDataFrame$NewCol %in% 
        c("Del","Del2")])
    

    And then use these to delete the corresponding rows:

    dataStart <- NewDataFrame[!(NewDataFrame$gvkey %in% keys_to_delete), ]
    
    0 讨论(0)
  • 2021-01-27 07:15
    set.seed(42)
    DF <- data.frame(a = sample(c("a", "b", "c"), 10, T), b = sample(1:10, 10, T))
    #    a  b
    # 1  c  5
    # 2  c  8
    # 3  a 10
    # 4  c  3
    # 5  b  5
    # 6  b 10
    # 7  c 10
    # 8  a  2
    # 9  b  5
    # 10 c  6
    
    
    library(plyr)
    res <- ddply(DF, .(a), transform, test = any(b %in% c(2, 3)))
    res[!res$test, 1:2]
    #   a  b
    # 3 b  5
    # 4 b 10
    # 5 b  5
    
    0 讨论(0)
  • 2021-01-27 07:24

    Use a bit of ave action, using the example data @Roland used:

    DF[ave(DF$b,DF$a, FUN=function(x) !any(x %in% c(2,3)))==1,]
    

    And an adaptation of Jan's nice answer:

    DF[!DF$a %in% unique(DF$a[DF$b %in% c(2,3)]) ,]
    

    Both giving:

      a  b
    5 b  5
    6 b 10
    9 b  5
    
    0 讨论(0)
提交回复
热议问题