How can I give number to data based on length of given columns?
1
0
Entering edit mode
3.3 years ago
star ▴ 350

I have a table like below that I would like to add another column in the data but based on the same regions in 3 first columns.

Input:

chr1    956563  956812    chr1    956563  956573
chr1    956563  956812    chr1    956573  956583
chr1    106563  106912    chr1    106583  106593
chr1    106563  106912    chr1    106653  106663
chr1    106563  106912    chr1    106663  106673
chr1    113563  113789    chr1    113763  113773
chr1    113563  113789    chr1    113573  113783
chr1    213563  213781    chr1    213663  213673

Output:

chr1    956563  956812    chr1    956563  956573     1
chr1    956563  956812    chr1    956573  956583     2
chr1    106563  106912    chr1    106583  106593     1
chr1    106563  106912    chr1    106653  106663     2
chr1    106563  106912    chr1    106663  106673     3
chr1    113563  113789    chr1    113763  113773     1
chr1    113563  113789    chr1    113573  113783     2
chr1    213563  213781    chr1    213663  213673     1

Thanks for any help in advance!

R bedtools • 768 views
ADD COMMENT
2
Entering edit mode
ADD REPLY
3
Entering edit mode
3.3 years ago

Tidyverse solution

library("dplyr")

df %>%
  group_by(across(1:3)) %>%
  mutate(count=seq(1, n()))

# A tibble: 8 x 7
# Groups:   V1, V2, V3 [4]
  V1        V2     V3 V4        V5     V6 count
  <chr>  <int>  <int> <chr>  <int>  <int> <int>
1 chr1  956563 956812 chr1  956563 956573     1
2 chr1  956563 956812 chr1  956573 956583     2
3 chr1  106563 106912 chr1  106583 106593     1
4 chr1  106563 106912 chr1  106653 106663     2
5 chr1  106563 106912 chr1  106663 106673     3
6 chr1  113563 113789 chr1  113763 113773     1
7 chr1  113563 113789 chr1  113573 113783     2
8 chr1  213563 213781 chr1  213663 213673     1
ADD COMMENT

Login before adding your answer.

Traffic: 1875 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6