I am trying to figure out how to remove all unique rows, from a data frame, but if it has a duplicate, I want that to stay in. For Example - I want all columns from this wi
Another option:
subset(df,duplicated(col1) | duplicated(col1, fromLast=TRUE))
You can do this by creating an index with ave
:
df[as.logical(ave(1:nrow(df), df$col1, FUN=function(x) length(x) > 1)), ]
produces
col1 col2 col3
1 a A 3
2 a B 3
3 a C 1
6 d A 3
7 d B 2
8 d C 1
Try:
> tdf <- table(df$col1)
a b c d
3 1 1 3
df[df$col1 %in% names(tdf)[tdf>1],]
> df
col1 col2 col3
1 a A 3
2 a B 3
3 a C 1
6 d A 3
7 d B 2
8 d C 1