[R] Adding rows based on column value

Bansal, Vikas vikas.bansal at kcl.ac.uk
Fri Jul 15 19:44:33 CEST 2011


Dear all,

I have one problem and did not find any solution.
I have attached the question in text file also because sometimes spacing is not good in mail.

I have a file(file.txt) attached with this mail.I am reading it using this code to make a data frame (file)-

file=read.table("file.txt",fill=T,colClasses = "character",header=T)

file looks like this-

 Chr       Pos    CaseA     CaseC    CaseG      CaseT
  10 135344110  0.000000 24.000000  0.000000  0.000000
  10 135344110  0.000000  0.000000 24.000000  0.000000
  10 135344110  0.000000  0.000000 24.000000  0.000000
  10 135344113  0.000000  0.000000 24.000000  0.000000
  10 135344114 24.000000  0.000000  0.000000  0.000000
  10 135344114 24.000000  0.000000  0.000000  0.000000
  10 135344116  0.000000  0.000000  0.000000 24.000000
  10 135344118  0.000000 24.000000  0.000000  0.000000
  10 135344118  0.000000  0.000000  0.000000 24.000000
  10 135344122 24.000000  0.000000  0.000000  0.000000
  10 135344122  0.000000 24.000000  0.000000  0.000000
  10 135344123  0.000000 24.000000  0.000000  0.000000
  10 135344123  0.000000 24.000000  0.000000  0.000000
  10 135344123  0.000000  0.000000  0.000000 24.000000
  10 135344126  0.000000  0.000000 24.000000  0.000000

Now some of the values in column Pos are same.For these same positions i want to add the values of columns 3:6
I will explain with an example-
The output of first row should be-

 Chr       Pos   CaseA     CaseC      CaseG      CaseT
 10 135344110  0.000000 24.000000  48.000000  0.000000

because first three rows have same value in Pos column.

so the whole output for above input should be-

 Chr       Pos    CaseA     CaseC         CaseG      CaseT
 10 135344110    0.000000  24.000000  48.000000    0.000000
 10 135344113    0.000000   0.000000   24.000000    0.000000
  10 135344114  48.000000  0.000000    0.000000     0.000000
  10 135344116   0.000000   0.000000    0.000000    24.000000
  10 135344118   0.000000  24.000000   0.000000    24.000000
  10 135344122  24.000000 24.000000   0.000000    0.000000
  10 135344123   0.000000  48.000000   0.000000    24.000000
  10 135344126   0.000000  0.000000    24.000000   0.000000

Can you please help me.


Thanking you,
Warm Regards
Vikas Bansal
Msc Bioinformatics
Kings College London
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: question.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110715/ce1d2191/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: file.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110715/ce1d2191/attachment-0001.txt>


More information about the R-help mailing list