r - finding duplicate rows counts by keeping all rows -
this question has answer here:
i have data frame df1
df1: c 1: 1 6 2: 2 8 3: 3 1 4: 45 3 5: 2 8
i need find duplicate count of rows keeping duplicate rows.the result should like:
c count 1: 1 6 1 2: 2 8 2 3: 3 1 1 4: 45 3 1 5: 2 8 2
as rows 2 , 5 duplicates.but able solution give answer
c count 1: 1 6 1 2: 2 8 2 3: 3 1 1 4: 45 3 1
by doing
df1<-data.table(df1) df1[, .n, = list(a,c)]
how desired result?
you may in base
r:
df1$count <- with(df1, ave(a, list(a, c), fun = length)) df1 # c count # 1: 1 6 1 # 2: 2 8 2 # 3: 3 1 1 # 4: 45 3 1 # 5: 2 8 2
Comments
Post a Comment