Question: Spliting a dataframe based in a chromosome
0
gravatar for viniciushs88
3.4 years ago by
viniciushs8850
Germany
viniciushs8850 wrote:

 

I would like to split my dataframe depending of a collumn tagged as "Chr". I would like to write these splited objetcs in a .txt file automatically too.

My input:

Name   Chr  Position LTR
Prob1   1     55     0.2
Prob2   2     25     0.9
Prob3   3     25     0.7
Prob4   1     45     0.5

My First output:

Name   Chr  Position  LTR 
Prob1   1      55     0.2
Prob4   1      45     0.5

My second output:

 Name   Chr  Position  LTR 
 Prob2   2     25      0.9

My last output:

Name   Chr  Position  LTR 
Prob3   3     25      0.7

I am trying to do somehing like:

outfile <- paste0("newsplit",i,".txt") SPLIT PROCEDURES write.table(all, outfile, sep=";")

Where "i" is the correspondent chromosome (value in a "Chr" collumn).

Cheers!

split chr R • 1.0k views
ADD COMMENTlink modified 3.4 years ago by Chris Miller18k • written 3.4 years ago by viniciushs8850
2
gravatar for Chris Miller
3.4 years ago by
Chris Miller18k
Washington University in St. Louis, MO
Chris Miller18k wrote:

There are many ways to do this. Here is one:

 

> a = data.frame(Chr=c(1,1,2,3),Position=c(1234,5678,3456,7890),LTR=c(2,3,4,5))
> a
  Chr Position LTR
1   1     1234   2
2   1     5678   3
3   2     3456   4
4   3     7890   5

> for(i in unique(a$Chr)){write.table(a[a$Chr==i,],paste("newsplit",i,".txt",sep=""),sep="\t",quote=F,row.names=F)}

ADD COMMENTlink modified 3.4 years ago • written 3.4 years ago by Chris Miller18k
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: 892 users visited in the last hour