0
$\begingroup$

I have a .txt file; I have 139 unique sample IDs in sampleID column

> head(mutations)
                                                                              sampleID chr     pos ref mut
4 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1  920552   G   A
5 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1350208   G   A
6 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1447367   T   G
7 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1909310   G   A
8 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2172675   G   C
9 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2846623   C   T
> 

> dim(mutations)
[1] 3574407       5
> 

I want to change name of sample from 1 to 139 in R or Linux terminal but I don't know how to do that, any help please?

Thanks

$\endgroup$

1 Answer 1

1
$\begingroup$

I believe that

uniqsampleID = 1:length(unique(mutations$sampleID))
names(uniqsampleID) = unique(mutations$sampleID)
mutations$sampleID = uniqsampleID[mutations$sampleID]

should work, assuming mutations is a data frame.


Explanation:

1:N creates a vector of length N, containing 1, 2, ..., N. So using 1:length(X) will do this for each item in X, so 1:length(unique(mutations$sampleID)) should give you a vector of 1:139.

Then names(uniqsampleID) = unique(mutations$sampleID) assigns to each of these numbers one of these sample IDs, which allows you to retrieve the sample number with uniqsampleID[mutations$sampleID].

$\endgroup$
4
  • $\begingroup$ Sorry that gives 3574407 sample IDs while I only have 139 samples for each I have 25 chromosome and a lot of mutations; I mean sampleID must contains maximum 139 unique numbers $\endgroup$
    – Angel
    Nov 7, 2019 at 14:00
  • 1
    $\begingroup$ Ah I see! I'll change it. $\endgroup$
    – Whitehot
    Nov 7, 2019 at 14:02
  • $\begingroup$ Yessssss worked thanks; Sorry in chr column I have the number of chromosome 1:23 plus to X and Y; How I can add chr prefix to this column? I mean instead of 1 I should have chr1 $\endgroup$
    – Angel
    Nov 7, 2019 at 14:17
  • 1
    $\begingroup$ Technically should be another question, but paste0() can handle that. It would probably be mutations$chr = paste0("chr", mutations$chr) $\endgroup$
    – Whitehot
    Nov 7, 2019 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.