Data overview:
> str(dataStart[c(\"gvkey\",\"DEF\",\"FittedRob\",\"NewCol\")])
\'data.frame\': 1000 obs. of 4 variables:
$ gvkey : int 1004 1004
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), ]
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
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