[R] counting identical data in a column

jim holtman jholtman at gmail.com
Mon Feb 4 20:55:08 CET 2008


Is this what you want?

> x <- read.table(textConnection("  chrN     start         end
+ 1 chr1  11122333  11122633
+ 2 chr1  11122333  11122633
+ 3 chr3  11122333  11122633
+ 8 chr3 111273334 111273634
+ 7 chr2  12122334  12122634
+ 4 chr1  21122377  21122677
+ 5 chr2  33122355  33122655
+ 6 chr2  33122355  33122655"), header=TRUE)
> x$count <- ave(x$start, x$start, FUN=length)
> x
  chrN     start       end count
1 chr1  11122333  11122633     3
2 chr1  11122333  11122633     3
3 chr3  11122333  11122633     3
8 chr3 111273334 111273634     1
7 chr2  12122334  12122634     1
4 chr1  21122377  21122677     1
5 chr2  33122355  33122655     2
6 chr2  33122355  33122655     2



On 2/4/08, joseph <jdsandjd at yahoo.com> wrote:
> Hi Peter
> I have the following data frame with chromosome name, start and end positions:
>   chrN     start         end
> 1 chr1  11122333  11122633
> 2 chr1  11122333  11122633
> 3 chr3  11122333  11122633
> 8 chr3 111273334 111273634
> 7 chr2  12122334  12122634
> 4 chr1  21122377  21122677
> 5 chr2  33122355  33122655
> 6 chr2  33122355  33122655
> I would like to count the positions that have the same start and add a new column with the count number;
> the new data frame should look like this:
>  chrN
>  start         end              count
> 1 chr1  11122333  11122633       3
> 2 chr1  11122333  11122633       3
> 3 chr3  11122333  11122633       3
> 8 chr3 111273334 111273634     1
> 7 chr2  12122334  12122634       1
> 4 chr1  21122377  21122677       1
> 5 chr2  33122355  33122655       2
> 6 chr2  33122355  33122655       2
> Can you please show me how to achieve this?
> Thanks
> Joseph
>
>
>      ____________________________________________________________________________________
> Be a better friend, newshound, and
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list