[R] Adding rows based on column value

Bert Gunter gunter.berton at gene.com
Thu Jul 14 17:54:53 CEST 2011


?tapply (in base R)
?aggregate  ?by  (wrapper for tapply)
?ave (in base R -- based on tapply)

Also package plyr (and several others, undoubtedly).

Also google on "R summarize data by groups" or similar gets many relevant hits.

-- Bert




2011/7/14 Bansal, Vikas <vikas.bansal at kcl.ac.uk>:
> Dear all,
>
> I have one problem and did not find any solution.(I have also attached the problem in text file because sometimes column 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 2: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
>
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list