[R] How can I get the Ids with Duplicated key and corresponding Ids with original key?

Jim Lemon jim at bitwrit.com.au
Mon Aug 13 12:07:12 CEST 2012


On 08/13/2012 07:17 PM, Sri krishna Devarayalu Balanagu wrote:
>
> In this following example Id 4 is duplicated with Id 1.
> Like this I want both Ids (Duplicated and Duplicated with). Can anyone help?
>
> df<- data.frame(
>      "Publication" = c(1, 2, 3, 1, 4, 5, 2, 3),
>      "Reference"   = c("a", "b", "c", "a", "d", "e", "b", "c"),
>      "Id"= c(1, 2, 3, 4, 5, 6, 7, 8)
>                   )
>
> key<- paste(df$Publication, df$Reference, sep="_")
> df.key<- cbind(key, df)
> Duplicated.ids<- df.key[duplicated(df.key$key), c("Id")]
>
Hi Sri krishna Devarayalu Balanagu,
Does this do it?

cat("Id Publication(s)\n")
for(pub in unique(df$Publication))
  cat(pub,"-",df$Id[which(df$Publication==pub)],"\n")

Jim



More information about the R-help mailing list