Question: How can I give number to data based on length of given columns?
0
gravatar for star
19 days ago by
star270
Netherlands
star270 wrote:

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 • 101 views
ADD COMMENTlink modified 19 days ago by rpolicastro3.3k • written 19 days ago by star270
2

Related SO post:

ADD REPLYlink written 19 days ago by zx87549.9k
3
gravatar for rpolicastro
19 days ago by
rpolicastro3.3k
Bloomington, IN
rpolicastro3.3k wrote:

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 COMMENTlink modified 19 days ago • written 19 days ago by rpolicastro3.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1580 users visited in the last hour
_