Home > Enterprise >  R: Frequency Table on Unique Rows in a Dataframe
R: Frequency Table on Unique Rows in a Dataframe

Time:11-24

I have a dataframe that looks like the following, where many are redundant rows. I'd like to collapse the dataframe so that there are only unique rows, but there is another variable weight that shows the number of times a unique row showed up.

A snippet of my dataframe:

from    to 
6140690 6141500
6140430 6140428
6141450 6140428
6141450 6140428
6140430 6141450
6140430 6141450

Desired output:

from    to      weight
6140690 6141500  1
6140430 6140428  1
6141450 6140428  2
6140430 6141450  2

CodePudding user response:

Here's an R base approach

 merge(unique(df), data.frame(table(df)))
     from      to Freq
1 6140430 6140428    1
2 6140430 6141450    2
3 6140690 6141500    1
4 6141450 6140428    2

Here's a dplyr approach using count:

  count(df, from, to, name="weight")
         from      to weight
    1 6140430 6140428      1
    2 6140430 6141450      2
    3 6140690 6141500      1
    4 6141450 6140428      2
  • Related