Find duplicated rows (based on 2 columns) in Data Frame in R

后端 未结 6 713
独厮守ぢ
独厮守ぢ 2020-11-27 06:10

I have a data frame in R which looks like:

| RIC    | Date                | Open   |
|--------|---------------------|--------|
| S1A.PA | 2011-06-30 20:00:00         


        
6条回答
  •  伪装坚强ぢ
    2020-11-27 06:52

    I think what you're looking for is a way to return a data frame of the duplicated rows in the same format as your original data. There is probably a more elegant way to do this but this works:

    dup <- data.frame(as.numeric(duplicated(df$var))) #creates df with binary var for duplicated rows
    colnames(dup) <- c("dup") #renames column for simplicity
    df2 <- cbind(df, dup) #bind to original df
    df3 <- subset(df2, dup == 1) #subsets df using binary var for duplicated`
    

提交回复
热议问题