[R] Adding rows based on column value

Bansal, Vikas vikas.bansal at kcl.ac.uk
Thu Jul 14 18:46:54 CEST 2011


I have checked it but did not get any results.Is there a way I can do it?I will be very thankful to you.

Thanking you,
Warm Regards
Vikas Bansal
Msc Bioinformatics
Kings College London
________________________________________
From: Bert Gunter [gunter.berton at gene.com]
Sent: Thursday, July 14, 2011 4:54 PM
To: Bansal, Vikas
Cc: r-help at r-project.org
Subject: Re: [R] Adding rows based on column value

?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 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
>
> 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