Help with script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with script

PABLO ORTIZ PINEDA
Hello there. Happy new year for everyone!

I need help with a table. This table contains 300 rows and 192 columns.
Being the first column the ID of my samples that can have several
observations.

I need to generate e NEW table that contains a single ID with the sum of
the observations by columns:
For example:

Example
ID   A    B    C    D    E    F    G.... 191 columns
a1   0    0    0    1    1    2    0...
a2   0    1    0    1    2    2    1...
a2   0    1    1    2    0    2    1...
a3   0    1    1    1    1    1    1....
...300rows
In this case I want to make a new table in which there is only 1 ID and
the values of each columns A...G are added. I
n the example the new table would have only 3 IDs. a1, a2 and 3 and a2
has the values added by column:
a2   0   2   1   3   2   4   2..

Thank you so much and have a wonderful year!.

--
Pablo A. Ortiz-Pineda (Ph.D.)
Molecular Biology & Bioinformatics
Yale University. School of Medicine.
Pediatrics Department.
New Haven, CT 06510


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with script

Rui Barradas
Hello,

Just use ?aggregate.

Example <- read.table(text = "
ID   A    B    C    D    E    F    G
a1   0    0    0    1    1    2    0
a2   0    1    0    1    2    2    1
a2   0    1    1    2    0    2    1
a3   0    1    1    1    1    1    1
", header = TRUE)

aggregate(. ~ ID, Example , sum)


Happy holidays,

Rui Barradas

On 12/29/2017 12:03 AM, PABLO ORTIZ PINEDA wrote:

> Hello there. Happy new year for everyone!
>
> I need help with a table. This table contains 300 rows and 192 columns.
> Being the first column the ID of my samples that can have several
> observations.
>
> I need to generate e NEW table that contains a single ID with the sum of
> the observations by columns:
> For example:
>
> Example
> ID   A    B    C    D    E    F    G.... 191 columns
> a1   0    0    0    1    1    2    0...
> a2   0    1    0    1    2    2    1...
> a2   0    1    1    2    0    2    1...
> a3   0    1    1    1    1    1    1....
> ...300rows
> In this case I want to make a new table in which there is only 1 ID and
> the values of each columns A...G are added. I
> n the example the new table would have only 3 IDs. a1, a2 and 3 and a2
> has the values added by column:
> a2   0   2   1   3   2   4   2..
>
> Thank you so much and have a wonderful year!.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with script

Eric Berger
In reply to this post by PABLO ORTIZ PINEDA
Hi Pablo,
There are probably many ways to do this in R. This suggestion uses dplyr.
The solution is actually only one line (see the line starting with dat2).
The first section simply creates the example data.

library(dplyr)
# 1. set up the example data
m <- matrix( c(0,0,0,0,0,1,1,1,0,0,1,1,1,1,2,1,1,2,0,1,2,2,2,1,0,1,1,1),
nrow=4)
dat <- as.data.frame(m)
dat$ID <- c("a1","a2","a2","a3")
dat <- dat[,c(8,1:7)]
colnames(dat) <- c("ID",LETTERS[1:7])

#2. group the data by ID, summing the columns in each group
dat2 <- group_by(dat,ID) %>% summarise_all( sum )

#3. show the results
dat2

# # A tibble: 3 x 8
#      ID     A     B     C     D     E     F     G
#   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1    a1     0     0     0     1     1     2     0
# 2    a2     0     2     1     3     2     4     2
# 3    a3     0     1     1     1     1     1     1

HTH,
Eric


On Fri, Dec 29, 2017 at 2:03 AM, PABLO ORTIZ PINEDA <[hidden email]>
wrote:

> Hello there. Happy new year for everyone!
>
> I need help with a table. This table contains 300 rows and 192 columns.
> Being the first column the ID of my samples that can have several
> observations.
>
> I need to generate e NEW table that contains a single ID with the sum of
> the observations by columns:
> For example:
>
> Example
> ID   A    B    C    D    E    F    G.... 191 columns
> a1   0    0    0    1    1    2    0...
> a2   0    1    0    1    2    2    1...
> a2   0    1    1    2    0    2    1...
> a3   0    1    1    1    1    1    1....
> ...300rows
> In this case I want to make a new table in which there is only 1 ID and
> the values of each columns A...G are added. I
> n the example the new table would have only 3 IDs. a1, a2 and 3 and a2
> has the values added by column:
> a2   0   2   1   3   2   4   2..
>
> Thank you so much and have a wonderful year!.
>
> --
> Pablo A. Ortiz-Pineda (Ph.D.)
> Molecular Biology & Bioinformatics
> Yale University. School of Medicine.
> Pediatrics Department.
> New Haven, CT 06510
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with script

Ek Esawi
In reply to this post by PABLO ORTIZ PINEDA
 Hi Pablo,

Rui's suggestion is probably the best and easiest. In addition to
Eric's, i think the apply functions will work well here too.

Best of luck,
EK

On Thu, Dec 28, 2017 at 7:03 PM, PABLO ORTIZ PINEDA
<[hidden email]> wrote:

> Hello there. Happy new year for everyone!
>
> I need help with a table. This table contains 300 rows and 192 columns.
> Being the first column the ID of my samples that can have several
> observations.
>
> I need to generate e NEW table that contains a single ID with the sum of
> the observations by columns:
> For example:
>
> Example
> ID   A    B    C    D    E    F    G.... 191 columns
> a1   0    0    0    1    1    2    0...
> a2   0    1    0    1    2    2    1...
> a2   0    1    1    2    0    2    1...
> a3   0    1    1    1    1    1    1....
> ...300rows
> In this case I want to make a new table in which there is only 1 ID and
> the values of each columns A...G are added. I
> n the example the new table would have only 3 IDs. a1, a2 and 3 and a2
> has the values added by column:
> a2   0   2   1   3   2   4   2..
>
> Thank you so much and have a wonderful year!.
>
> --
> Pablo A. Ortiz-Pineda (Ph.D.)
> Molecular Biology & Bioinformatics
> Yale University. School of Medicine.
> Pediatrics Department.
> New Haven, CT 06510
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.