Math
Math

Reputation: 1294

put duplicated rows in different data.frame(s)

Let

x=c(1,2,2,3,4,1)
y=c("A","B","C","D","E","F")
df=data.frame(x,y)



 df
      x y
    1 1 A
    2 2 B
    3 2 C
    4 3 D
    5 4 E
    6 1 F

How can I put duplicate rows in this data frame in different data frames like this :

df1
     x y
     1 A
     1 F

df2
     x y
     2 B
     2 C 

Thank you for help

Upvotes: 1

Views: 464

Answers (1)

Veerendra Gadekar
Veerendra Gadekar

Reputation: 4472

You could use split

split(df, f = df$x) 

f = df$x is used to specify the grouping column check ?split for more details

to remove the non duplicated rows you could use

mylist = split(df, f = df$x)[df$x[duplicated(df$x)]]
names(mylist) = c('df1', 'df2') 
list2env(mylist,envir=.GlobalEnv) # to separate the data frames 

Upvotes: 3

Related Questions