Skip to main content

Posts

Showing posts with the label Duplicates

How to add column with number of occurances in dataset?

I have a dataframe in R with many rows that are duplicate: header1 header2 tuna apple orange salmon orange trout blue trout orange salmon tuna apple As you can see, rows 1 & 6 and 2 & 5 are exact duplicates of each other. I would like to remove the duplicates, but create a third column where it lists the number of occurrences: header1 header2 NEW: Number of Occurances tuna apple 2 orange salmon 2 orange trout 1 blue trout 1 Does anyone have an idea for how I can do this? I'm really stuck. Any help is much appreciated. --------- [I did take multiple shots at it. I don't think they're close to being correct, but here's what I was trying]: countx = 1 county = 2 while (county <= 6){ if ((df[countx,] == df[county,]) == T & T){ print("true") } else { print ("false") } county <- county+1 } However this will return "true" even if onl...