I have a data frame like this:
ID Exp1 Exp2 Value1
AAA 5 6 7
AAA 4 8 8
BBB 3 5 9
BBB 6 7 4
CCC 2 5 6
....
and I would like to create a new row after every repetition of an ID,and do summation of previous results, like this:
ID Exp1 Exp2 Value1
AAA 5 6 7
AAA 4 8 8
AAA.1 9 14 15
BBB 3 5 9
BBB 6 7 4
BBB.1 9 12 13
CCC 2 5 6
...
My problem is I cannot write a code to insert a new row right after same IDs.
> for (i in 1:nrow(Data)) {
> temp1 <- Data[Data$ID == Data$ID[i],]
but do not know how to proceed... Any ideas?
Update: how the original data is..
GeneNames Original ID2 Com. Ratio Cyt Nuc
YWHAB CL84Contig6 1433B_HUMAN -0.2 0.6 1063.3 671.3
YWHAB CL84Contig4 1433B_HUMAN -0.3 0.5 59.0 30.5
YWHAE CL1665Contig1 1433E_HUMAN -0.3 0.5 2784.6 1490.1
YWHAE CL1665Contig4 1433E_HUMAN 0.1 1.2 2.1 4.8
YWHAH dsrrswapns 1433F_HUMAN 0.0 0.0 0.0 0.0
YWHAG CL2762Contig2 1433G_HUMAN -0.3 0.4 39.5 17.7
YWHAG CL2762Contig3 1433G_HUMAN 0.0 0.0 0.0 0.0
how I would like to do that...
GeneNames Original ID2 Com. Ratio Cyt Nuc
YWHAB CL84Contig6 1433B_HUMAN -0.2 0.6 1063.3 671.3
YWHAB CL84Contig4 1433B_HUMAN -0.3 0.5 59.0 30.5
YWHAB.1 CL84Contig6 1433B_HUMAN -0.2 0.6 1122.4 701.8
YWHAE CL1665Contig1 1433E_HUMAN -0.3 0.5 2784.6 1490.1
YWHAE CL1665Contig4 1433E_HUMAN 0.1 1.2 2.1 4.8
YWHAE.1 CL1665Contig1 1433E_HUMAN -0.3 0.5 2786.6 1494.9
I have a data.frame: 13044 obs. of 94 variables: these 94 variables are num and chr columns.. I would like to sum up values only from Cyt and Nuc from same GeneNames, and write them into new row where GeneName is named "GeneName.1". Rest of the columns are not same for each GeneName. I would prefer to leave them either empty or copy the first column of the same GeneName, as in the example..