-3

so I wanted to calculate the sum of each column and the results are shifted. I have no idea how to handle it.

My data:

> head(df)

K    2  5  4  2
L    2  1  4  1
M    1  3  4  3
N    3  2  1  1
Sum  7  8  11 13

so as you see the results are not proper. The sum of first column is in the second column and in the first column is the sum of the last one. How to handle it ?

I used that code to calculate the sum:

df <- suppressWarnings(rbind(data, Sum=colSums(data[, -1])))

That how my data looks like:

> dput(head(data,4))
structure(list(Name = structure(c(95L, 331L, 161L, 156L
), .Label = c(" 1-deoxy-D-xylulose 5-phosphate reductoisomerase ", 
" 2-cysteine peroxiredoxin B ", " 2-oxoacid dehydrogenases acyltransferase family protein ", 
" 2-oxoglutarate (2OG) and Fe(II)-dependent oxygenase superfamily protein ", 
" 26S proteasome, regulatory subunit Rpn7;Proteasome component (PCI) domain ", 
" 3-dehydroquinate synthase, putative ", " 3-ketoacyl-acyl carrier protein synthase I ", 
" 3-ketoacyl-acyl carrier protein synthase III ", " 4-hydroxy-3-methylbut-2-enyl diphosphate reductase ", 
" 40s ribosomal protein SA ", " 5'adenylylphosphosulfate reductase 2 ", 
" AAA-type ATPase family protein ", " ACC oxidase 2 ", " acetoacetyl-CoA thiolase 2 ", 
" ACT domain-containing small subunit of acetolactate synthase protein ", 
" actin 7 ", " actin 8 ", " adenosine kinase 1 ", " adenosine kinase 2 ", 
" adenylosuccinate synthase ", " ADP-glucose pyrophosphorylase family protein ", 
" ADP glucose pyrophosphorylase  1 ", " ADP/ATP carrier 1 ", 
" Aha1 domain-containing protein ", " alanine:glyoxylate aminotransferase ", 
" alanine:glyoxylate aminotransferase 2 ", " Alba DNA/RNA-binding protein ", 
" Aldolase-type TIM barrel family protein ", " Aldolase superfamily protein ", 
" alkenal reductase ", " allene oxide synthase ", " Alpha-S1 casein precursor - Bos taurus (Bovine).", 
" alpha/beta-Hydrolases superfamily protein ", " amidase 1 ", 
" Amino acid dehydrogenase family protein ", " ankyrin repeat-containing 2B ", 
" ankyrin repeat-containing protein 2 ", " Ankyrin repeat family protein ", 
" annexin 1 ", " annexin 7 ", " APS reductase 3 ", " Arginase/deacetylase superfamily protein ", 
" aspartate aminotransferase 1 ", " aspartate aminotransferase 2 ", 
" aspartate aminotransferase 3 ", " aspartate aminotransferase 5 ", 
" ATP-dependent caseinolytic (Clp) protease/crotonase family protein ", 
" ATP citrate lyase (ACL) family protein ", " ATP phosphoribosyl transferase 1 ", 
" ATP phosphoribosyl transferase 2 ", " ATP sulfurylase 1 ", 
" ATP synthase alpha/beta family protein ", " ATP synthase protein I -related ", 
" ATP synthase subunit alpha ", " ATP synthase subunit beta ", 
" ATPase, F0 complex, subunit B/B', bacterial/chloroplast ", 
" ATPase, F1 complex, alpha subunit protein ", " ATPase, F1 complex, gamma subunit protein ", 
" ATPase, V0/A0 complex, subunit C/D ", " ATPase, V1 complex, subunit B protein ", 
" basic transcription factor 3 ", " beta-1,3-glucanase_putative ", 
" Bifunctional inhibitor/lipid-transfer protein/seed storage 2S albumin superfamily protein ", 
" binding to TOMV RNA 1L (long form) ", " branched-chain amino acid aminotransferase 5 / branched-chain amino acid transaminase 5 (BCAT5) ", 
" branched-chain aminotransferase 3 ", " branched-chain aminotransferase4 ", 
" calcium sensing receptor ", " carbonic anhydrase 1 ", " carbonic anhydrase 2 ", 
" catalase 2 ", " chaperonin-60alpha ", " chaperonin 60 beta ", 
" chlorophyll A/B binding protein 3 ", " chloroplast heat shock protein 70-1 ", 
" chloroplast heat shock protein 70-2 ", " chloroplast RNA binding ", 
" chloroplast stem-loop binding protein of  41 kDa ", " chloroplastic NIFS-like cysteine desulfurase ", 
" chorismate synthase, putative / 5-enolpyruvylshikimate-3-phosphate phospholyase, putative ", 
" cinnamyl alcohol dehydrogenase 9 ", " Citrate synthase family protein ", 
" Class I glutamine amidotransferase-like superfamily protein ", 
" Clp ATPase ", " Coatomer, alpha subunit ", " Cobalamin-independent synthase family protein ", 
" cold, circadian rhythm, and RNA binding 1 ", " cold, circadian rhythm, and rna binding 2 ", 
" Coproporphyrinogen III oxidase ", " Cupredoxin superfamily protein ", 
" Cyclophilin-like peptidyl-prolyl cis-trans isomerase family protein ", 
" cyclophilin 38 ", " cysteine synthase 26 ", " cysteine synthase C1 ", 
" Cytosol aminopeptidase family protein ", " cytosolic NADP+-dependent isocitrate dehydrogenase ", 
" D-3-phosphoglycerate dehydrogenase ", " D-cysteine desulfhydrase ", 
" D-ribulose-5-phosphate-3-epimerase ", " DegP protease 1 ", 
" Dehydrin family protein ", " delta tonoplast integral protein ", 
" desulfo-glucosinolate sulfotransferase 18 ", " Di-glucose binding protein with Kinesin motor domain ", 
" dicarboxylate diiron protein, putative (Crd1) ", " dicarboxylate transport 2.1 ", 
" dicarboxylate transporter 1 ", " Dihydrolipoamide succinyltransferase ", 
" Disease resistance protein (TIR-NBS-LRR class) family ", " DNA repair ATPase-related ", 
" DNAJ heat shock N-terminal domain-containing protein ", " Domain of unknown function (DUF3598) ", 
" dual specificity protein phosphatase (DsPTP1) family protein ", 
" edited nad9/rpl16 transcript found in intergenic region. From Philippe Giege (CNRS)", 
" edited PSBE", " eif4a-2 ", " elicitor-activated gene 3-1 ", 
" Enolase ", " epithiospecifier modifier 1 ", " epithiospecifier protein ", 
" ethylene-dependent gravitropism-deficient and yellow-green-like 2 ", 
" ethylene-forming enzyme ", " Eukaryotic aspartyl protease family protein ", 
" eukaryotic initiation factor 4A-III ", " eukaryotic translation initiation factor 2 alpha subunit ", 
" Eukaryotic translation initiation factor 2 subunit 1 ", " eukaryotic translation initiation factor 4A1 ", 
" FASCICLIN-like arabinogalactan protein 13 precursor ", " FASCICLIN-like arabinoogalactan 9 ", 
" ferredoxin-NADP(+)-oxidoreductase 1 ", " ferredoxin-NADP(+)-oxidoreductase 2 ", 
" flavanone 3-hydroxylase ", " formate dehydrogenase ", " fructose-bisphosphate aldolase 1 ", 
" fructose-bisphosphate aldolase 2 ", " FTSH protease 1 ", " FUNCTIONS IN: molecular_function unknown; INVOLVED IN: biological_process unknown; LOCATED IN: cyt", 
" GDP-D-mannose 3',5'-epimerase ", " GDSL-like Lipase/Acylhydrolase superfamily protein ", 
" germin 3 ", " Glucose-1-phosphate adenylyltransferase family protein ", 
" glutamate-1-semialdehyde-2,1-aminomutase ", " glutamate-1-semialdehyde 2,1-aminomutase 2 ", 
" glutamate-cysteine ligase ", " glutamate synthase 1 ", " glutamate:glyoxylate aminotransferase ", 
" glutamine synthase clone F11 ", " glutamine synthetase 2 ", 
" glyceraldehyde-3-phosphate dehydrogenase B subunit ", " glyceraldehyde-3-phosphate dehydrogenase C subunit 1 ", 
" glyceraldehyde-3-phosphate dehydrogenase C2 ", " glyceraldehyde-3-phosphate dehydrogenase of plastid 2 ", 
" glyceraldehyde 3-phosphate dehydrogenase A subunit ", " glyceraldehyde 3-phosphate dehydrogenase A subunit 2 ", 
" Glycine cleavage T-protein family ", " glycine decarboxylase P-protein 1 ", 
" GroES-like zinc-binding alcohol dehydrogenase family protein ", 
" GroES-like zinc-binding dehydrogenase family protein ", " GTP-binding protein-related ", 
" GTP binding ", " GTP binding Elongation factor Tu family protein ", 
" heat shock protein 70 (Hsp 70) family protein ", " Heat shock protein 70 (Hsp 70) family protein ", 
" heat shock protein 90.1 ", " high chlorophyll fluorescent 109 ", 
" high cyclic electron flow 1 ", " histidinol dehydrogenase ", 
" Histone superfamily protein ", " homolog of bacterial cytokinesis Z-ring protein FTSZ 1-1 ", 
" homoserine kinase ", " HOPW1-1-interacting 1 ", " HSP20-like chaperones superfamily protein ", 
" HXXXD-type acyl-transferase family protein ", " Hyaluronan / mRNA binding family ", 
" hydroxymethylbilane synthase ", " hydroxyproline-rich glycoprotein family protein ", 
" hydroxypyruvate reductase ", " Inositol monophosphatase family protein ", 
" Insulinase (Peptidase family M16) protein ", " Involved in response to salt stress.  Knockout mutants are hypersensitive to salt stress. ", 
" Iron-sulphur cluster biosynthesis family protein ", " isocitrate dehydrogenase subunit 2 ", 
" isocitrate dehydrogenase V ", " isocitrate dehydrogenase VI ", 
" isopropyl malate isomerase large subunit 1 ", " isopropylmalate dehydrogenase 1 ", 
" isopropylmalate dehydrogenase 2 ", " isopropylmalate dehydrogenase 3 ", 
" Keratin 1 - Homo sapiens (Human).", " Keratin 10 - Homo sapiens (Human).", 
" Keratin 14 (Epidermolysis bullosa simplex, Dowling-Meara, Koebner) - Homo sapiens (Human).", 
" Keratin 2a - Homo sapiens (Human).", " Keratin 5 - Homo sapiens (Human).", 
" Keratin, type I cytoskeletal 9 (Cytokeratin 9) (K9) (CK 9) - Homo sapiens (Human).", 
" Keratin, type II cytoskeletal 2 epidermal (Cytokeratin 2e) (K2e) (CK 2e) - Homo sapiens (Human).", 
" ketol-acid reductoisomerase ", " lactate/malate dehydrogenase family protein ", 
" Lactate/malate dehydrogenase family protein ", " Late embryogenesis abundant protein, group 2 ", 
" Leucine-rich repeat (LRR) family protein ", " Leucine-rich repeat protein kinase family protein ", 
" light-harvesting chlorophyll-protein complex I subunit A4 ", 
" light harvesting complex of photosystem II 5 ", " light harvesting complex photosystem II ", 
" light harvesting complex photosystem II subunit 6 ", " lipoxygenase 2 ", 
" magnesium chelatase i2 ", " malate dehydrogenase ", " Mannose-binding lectin superfamily protein ", 
" MAP kinase 11 ", " metallopeptidase M24 family protein ", " methionine adenosyltransferase 3 ", 
" Molecular chaperone Hsp40/DnaJ family protein ", " Molybdenum cofactor sulfurase family protein ", 
" monodehydroascorbate reductase 1 ", " monodehydroascorbate reductase 6 ", 
" Mov34/MPN/PAD-1 family protein ", " myosin heavy chain-related ", 
" NAD(P)-binding Rossmann-fold superfamily protein ", " NAD(P)-linked oxidoreductase superfamily protein ", 
" NAD(P)H dehydrogenase C1 ", " NAD(P)H dehydrogenase subunit H ", 
" NADH dehydrogenase subunit 7 ", " NagB/RpiA/CoA transferase-like superfamily protein ", 
" NDH-dependent cyclic electron flow 1 ", " nitrilase 1 ", " Nitrilase/cyanide hydratase and apolipoprotein N-acyltransferase family protein ", 
" nitrile specifier protein 3 ", " nitrite reductase 1 ", " non-ATPase subunit 9 ", 
" non-intrinsic ABC protein 6 ", " non-photochemical quenching 1 ", 
" nuclear factor Y, subunit A2 ", " Nucleic acid-binding proteins superfamily ", 
" Nucleic acid-binding, OB-fold-like protein ", " nucleobase-ascorbate transporter 7 ", 
" Nucleotidylyl transferase superfamily protein ", " O-acetylserine (thiol) lyase B ", 
" O-acetylserine (thiol) lyase isoform C ", " O-methyltransferase 1 ", 
" O-methyltransferase family protein ", " ornithine carbamoyltransferase ", 
" Oxidoreductase family protein ", " Oxidoreductase, zinc-binding dehydrogenase family protein ", 
" oxidoreductases, acting on the aldehyde or oxo group of donors, NAD or NADP as acceptor;copper ion", 
" oxophytodienoate-reductase 3 ", " P-loop containing nucleoside triphosphate hydrolases superfamily protein ", 
" Pectinacetylesterase family protein ", " Peptidase M20/M25/M40 family protein ", 
" Peptide chain release factor 1 ", " peroxidase CB ", " Peroxidase superfamily protein ", 
" peroxisomal 3-keto-acyl-CoA thiolase 2 ", " peroxisomal 3-ketoacyl-CoA thiolase 3 ", 
" peroxisomal NAD-malate dehydrogenase 2 ", " pfkB-like carbohydrate kinase family protein ", 
" phenylalanyl-tRNA synthetase class IIc family protein ", " phosphatase-related ", 
" phosphoglycerate kinase ", " phosphoglycerate kinase 1 ", " Phosphoglycerate kinase family protein ", 
" phospholipid/glycerol acyltransferase family protein ", " phosphoribulokinase ", 
" phosphoserine aminotransferase ", " photosynthetic electron transfer A ", 
" photosynthetic electron transfer C ", " photosystem I light harvesting complex gene 3 ", 
" photosystem I subunit D-2 ", " photosystem I subunit E-2 ", 
" photosystem I subunit F ", " Photosystem I, PsaA/PsaB protein ", 
" photosystem II reaction center protein A ", " photosystem II reaction center protein B ", 
" photosystem II reaction center protein C ", " photosystem II reaction center protein D ", 
" photosystem II stability/assembly factor, chloroplast (HCF136) ", 
" photosystem II subunit O-2 ", " photosystem II subunit P-1 ", 
" photosystem II subunit Q-2 ", " photosystem II subunit QA ", 
" phragmoplastin interacting protein 1 ", " Plant stearoyl-acyl-carrier-protein desaturase family protein ", 
" plasma-membrane associated cation-binding protein 1 ", " plasma membrane intrinsic protein 1C ", 
" plasma membrane intrinsic protein 2A ", " Plastid-lipid associated protein PAP / fibrillin family protein ", 
" plastid transcriptionally active 16 ", " plastid transcriptionally active 17 ", 
" plastid transcriptionally active 5 ", " prohibitin 2 ", " Proteasome component (PCI) domain protein ", 
" Protein kinase superfamily protein ", " Protein of unknown function (DUF677) ", 
" Protein of unknown function, DUF642 ", " Protein phosphatase 2C family protein ", 
" protochlorophyllide oxidoreductase B ", " protochlorophyllide oxidoreductase C ", 
" Pyridine nucleotide-disulphide oxidoreductase family protein ", 
" Pyridoxal-dependent decarboxylase family protein ", " Pyridoxal phosphate (PLP)-dependent transferases superfamily protein ", 
" pyrimidine 1 ", " pyrimidine d ", " pyruvate dehydrogenase complex E1 alpha subunit ", 
" pyruvate dehydrogenase E1 alpha ", " pyruvate dehydrogenase E1 beta ", 
" RAB GTPase homolog E1B ", " regulatory particle triple-A ATPase 5A ", 
" reversibly glycosylated polypeptide 1 ", " Ribose 5-phosphate isomerase, type A protein ", 
" ribosomal protein 1 ", " ribosomal protein L12-A ", " Ribosomal protein L3 family protein ", 
" Ribosomal protein L4/L1 family ", " ribosomal protein L5 ", 
" Ribosomal protein L6 family protein ", " ribosomal protein S1 ", 
" Ribosomal protein S11 family protein ", " ribosomal protein S12A ", 
" Ribosomal protein S13/S18 family ", " Ribosomal protein S4 (RPS4A) family protein ", 
" Ribosomal protein S5 family protein ", " Ribosomal protein S5/Elongation factor G/III/V family protein ", 
" ribosomal protein S6 ", " ribulose-bisphosphate carboxylases ", 
" Ribulose bisphosphate carboxylase (small chain) family protein ", 
" ribulose bisphosphate carboxylase small chain 1A ", " RNA-binding (RRM/RBD/RNP motifs) family protein ", 
" RNA 3'-terminal phosphate cyclase/enolpyruvate transferase, alpha/beta ", 
" rotamase CYP 4 ", " rubisco activase ", " S-adenosyl-l-homocysteine (SAH) hydrolase 2 ", 
" S-adenosylmethionine synthetase 2 ", " S-adenosylmethionine synthetase family protein ", 
" S-domain-1 13 ", " Sec1/munc18-like (SM) proteins superfamily ", 
" SecY protein transport family protein ", " sedoheptulose-bisphosphatase ", 
" semialdehyde dehydrogenase family protein ", " SERINE CARBOXYPEPTIDASE-LIKE 49 ", 
" serine hydroxymethyltransferase 4 ", " serine transhydroxymethyltransferase 1 ", 
" SNARE associated Golgi protein family ", " sodium/calcium exchanger family protein / calcium-binding EF hand family protein ", 
" solanesyl diphosphate synthase 2 ", " spermidine synthase 1 ", 
" spermidine synthase 2 ", " SPFH/Band 7/PHB domain-containing membrane-associated protein family ", 
" sterol methyltransferase 2 ", " structural constituent of ribosome ", 
" sulfite oxidase ", " sulfotransferase 17 ", " TCP-1/cpn60 chaperonin family protein ", 
" tetratricopeptide repeat (TPR)-containing protein ", " Tetratricopeptide repeat (TPR)-like superfamily protein ", 
" Thiamin diphosphate-binding fold (THDP-binding) superfamily protein ", 
" thiazole biosynthetic enzyme, chloroplast (ARA6) (THI1) (THI4) ", 
" thioredoxin family protein ", " Thioredoxin superfamily protein ", 
" thylakoid rhodanese-like ", " thylakoidal ascorbate peroxidase ", 
" TRAF-like family protein ", " Transketolase ", " Transketolase family protein ", 
" Translation elongation factor EF1B, gamma chain ", " translation initiation factor 3 subunit H1 ", 
" triacylglycerol lipase-like 1 ", " trigalactosyldiacylglycerol2 ", 
" Tropomyosin-related ", " Trypsin family protein with PDZ domain ", 
" Trypsin precursor (EC 3.4.21.4) - Sus scrofa (Pig).", " tryptophan biosynthesis 1 ", 
" tryptophan synthase beta-subunit 2 ", " tubulin alpha-3 ", 
" tubulin alpha-4 chain ", " tubulin beta chain 2 ", " tubulin beta chain 4 ", 
" Tubulin/FtsZ family protein ", " Tyrosine transaminase family protein ", 
" UDP-D-apiose/UDP-D-xylose synthase 1 ", " UDP-D-apiose/UDP-D-xylose synthase 2 ", 
" UDP-D-glucose/UDP-D-galactose 4-epimerase 1 ", " UDP-glucosyl transferase 74B1 ", 
" Uncharacterised conserved protein (UCP030210) ", " unknown protein; BEST Arabidopsis thaliana protein match is: unknown protein (TAIR:AT1G16520.1); H", 
" unknown protein; FUNCTIONS IN: molecular_function unknown; INVOLVED IN: biological_process unknown", 
" unknown protein; INVOLVED IN: biological_process unknown; LOCATED IN: chloroplast stroma, chloropl", 
" unknown protein; LOCATED IN: chloroplast; EXPRESSED IN: 22 plant structures; EXPRESSED DURING: 13 ", 
" Uroporphyrinogen decarboxylase ", " vacuolar ATP synthase subunit A ", 
" vacuolar ATP synthase subunit C (VATC) / V-ATPase C subunit / vacuolar proton pump C subunit (DET3", 
" vacuolar ATP synthase subunit H family protein ", " VALINE-TOLERANT 1 "
), class = "factor"), timetE1_1rerun = c(6692.50136510743, 41682.9111356503, 
405946.374877924, 4640.34876265179), timetE2_1 = c(14404.8414547167, 
40466.9047986558, 638019.540242027, 2397.71968447607), time1 = c(10146.3608040476, 
34148.4389867747, 459639.431186888, 10490.8359468475), time2 = c(5951.82142983062, 
45616.664822506, 658598.827475944, 3961.96726959746), time3 = c(4048.60493244488, 
45729.9856907985, 639686.154465906, 4466.15324797652), time4 = c(59094.47562632, 
139889.207295753, 1764684.16499658, 26250.3247678529), time5 = c(27675.589438082, 
111309.636536099, 1117027.2897719, 20320.0802981216), time6 = c(26374.057451648, 
129781.168773572, 1147967.44539752, 18413.5391459416), time7 = c(43310.0075838041, 
96924.6162599508, 1156442.475223, 29061.2479135303), a_mean = c(7774.44229552491, 
43374.1166119026, 585562.72426545, 3866.54724117546), b_mean = c(39113.5325249635, 
119476.157216344, 1296530.34384725, 23511.2980313616), c_mean = c(23443.9874102442, 
81425.1369141232, 941046.53405635, 13688.9226362685)), .Names = c("Name", 
"time1", "time2", "time3", "time4", 
"time5", "time6", "time7", "time8", "time9", 
"a_mean", "b_mean", "c_mean"), row.names = c(NA, 4L), class = "data.frame")

The results of the sum:

data[445,]
        Name time1 time2 time3 time4 time5 time6 time7
colSums        <NA>      313508423 268165038      251963845 239573376 282568199 249946497 246142814
        time8 time9  a_mean  b_mean  c_mean
colSums 208003454 256074474 246665241 251369858 219252253

I calculated it in the Excell aswell so that's why I know it's shifted. I checked it like 10 times and I am pretty sure that the results are shifted. The sum value of c_mean should be in the first column (time1).

Shaxi Liver
  • 1,052
  • 3
  • 25
  • 47
  • why are removing the first column? I mean you should do this `rbind(data, Sum=colSums(data))`? – agstudy Nov 05 '13 at 15:12
  • Can you supply the head of `data`? – Jonas Tundo Nov 05 '13 at 15:12
  • 1
    @agstudy. Probably because the first column of `data` is not numeric. – Jonas Tundo Nov 05 '13 at 15:13
  • @JT85 I see. `rbind(data[,-1], Sum=colSums(data[, -1]))`...but better to remove the first column and set it as a rownames. – agstudy Nov 05 '13 at 15:17
  • 2
    Why are you suppressing warnings? If you didn't and you read the warnings R was probably telling you that you were rbinding things together of different lengths and it was going to recycle the shorter vector to match the length of the longer vector. – Simon O'Hanlon Nov 05 '13 at 15:27
  • The data I have is different. I just showed my problem on the example. I removed first column because it's not numeric. – Shaxi Liver Nov 05 '13 at 15:46
  • @ShaxiLiver what is the sum of your second and last columns? Are they the same? – Simon O'Hanlon Nov 05 '13 at 15:47
  • Nope, they are different. They're really big numbers it's like 5 x 10^8 or even more that's why I used the example. I removed the first column (with names) and now it works fine but I really would like to do that without removing the first column. – Shaxi Liver Nov 05 '13 at 15:54
  • I edited the first post. It's a data.frame. The function which you sent doesn't work. It gives different value, no idea why. – Shaxi Liver Nov 05 '13 at 16:13
  • I really don't know why people downvote something without explanation. My problem it's not solved yet and as I see noone knows how to do it properly. I gave a "reproducible example" by following this [link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Shaxi Liver Nov 05 '13 at 16:51
  • 1
    @ShaxiLiver, I'm curious: do you also go by the name of [Rechlay](http://stackoverflow.com/users/2920032/rechlay) here on Stack Overflow? – A5C1D2H2I1M1N2O1R2T1 Nov 05 '13 at 17:10

2 Answers2

5

Noting your actual attempted solution posted in the comment to @ChristopherLouden's answer, which looks suspiciously like the solution offered by @Jilber to a question from earlier today, I can finally reproduce your problem and offer a solution.

For the sake of simplicity, here's a much smaller data.frame to start our work with. Note that the data.frame has two non-numeric columns (one character and one factor). Something as small as this is sufficient to demonstrate your problem and is much easier for others to follow.

data <- structure(list(Name = c("a", "b", "c", "d"), 
    time1 = c(6692.50136510743, 41682.9111356503, 405946.374877924, 
    4640.34876265179), time2 = c(14404.8414547167, 40466.9047986558, 
    638019.540242027, 2397.71968447607), time3 = c(10146.3608040476, 
    34148.4389867747, 459639.431186888, 10490.8359468475), 
    New = structure(1:4, .Label = c("A", "B", "C", "D"), 
    class = "factor")), .Names = c("Name", "time1", "time2", "time3", 
    "New"), class = "data.frame", row.names = c(NA, 4L))
data
#   Name      time1     time2     time3 New
# 1    a   6692.501  14404.84  10146.36   A
# 2    b  41682.911  40466.90  34148.44   B
# 3    c 405946.375 638019.54 459639.43   C
# 4    d   4640.349   2397.72  10490.84   D

Here is your current solution, complete with strange "shifting" of column means.

df <- suppressWarnings(
  rbind(data, colMeans=colMeans(data[, sapply(data, is.numeric)])))
df
#                      Name      time1     time2     time3  New
# 1                       a   6692.501  14404.84  10146.36    A
# 2                       b  41682.911  40466.90  34148.44    B
# 3                       c 405946.375 638019.54 459639.43    C
# 4                       d   4640.349   2397.72  10490.84    D
# colMeans 114740.534035333 173822.252 128606.27 114740.53 <NA>

The solution I'm offering makes use of rbind.fill from "plyr" to bind the results to your original data.frame. The results are calculated only on the numeric columns of your original data.frame.

library(plyr) ## For `rbind.fill`
useme <- sapply(data, is.numeric)
rbind.fill(data, data.frame(t(colMeans(data[useme]))))
#   Name      time1     time2     time3  New
# 1    a   6692.501  14404.84  10146.36    A
# 2    b  41682.911  40466.90  34148.44    B
# 3    c 405946.375 638019.54 459639.43    C
# 4    d   4640.349   2397.72  10490.84    D
# 5 <NA> 114740.534 173822.25 128606.27 <NA>

mean(data$time1) ## Just for verification...
# [1] 114740.5
Community
  • 1
  • 1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
0

The correct code is:

df <- suppressWarnings(rbind(data, Sum=colSums(data)))

The first printed column is actually the row names which is not stored as a column of data. You can tell this because when you named Sum, it printed beneath K, L, M and N.

Christopher Louden
  • 7,540
  • 2
  • 26
  • 29
  • df <- suppressWarnings(rbind(data, colMeans=colMeans(data[, sapply(data, is.numeric)]))). I had to do like that because the first column is a part of the data. That's why I have to ignore first column. – Shaxi Liver Nov 05 '13 at 15:45
  • What warnings are you suppressing? That may help find the issue. – Christopher Louden Nov 05 '13 at 17:07