Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>R does have a function <code>addmargins</code> that lets you do something like this, but it expects a <code>table</code> or <code>matrix</code> as the input.</p> <pre><code>addmargins(as.matrix(mydf), 1, FUN = mean) # timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean # 1 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99 # 2 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14 # 3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53 # 4 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92 # mean 173482.724 497479.54 319083.15 330634.05 331434.59 160144.458 369657.83 264901.15 </code></pre> <hr> <h3>Update</h3> <p>There is an <a href="https://stackoverflow.com/q/19792110/1270695">almost identical (conceptually) question here</a>, and I thought I would share my answer from there here too.</p> <p>Assume we're starting with:</p> <pre><code>mydf &lt;- structure(list(Description = c("A", "B", "C", "D"), timetE4_1 = c(4048.605, 45729.986, 639686.154, 4466.153), Boo = structure(1:4, .Label = c("a", "b", "c", "d"), class = "factor"), timetE1_2 = c(59094.48, 139889.21, 1764684.16, 26250.32), timetE2_2 = c(27675.59, 111309.64, 1117027.29, 20320.08), Baa = c(FALSE, FALSE, TRUE, NA)), .Names = c("Description", "timetE4_1", "Boo", "timetE1_2", "timetE2_2", "Baa"), row.names = c("1", "2", "3", "4"), class = "data.frame") mydf # Description timetE4_1 Boo timetE1_2 timetE2_2 Baa # 1 A 4048.605 a 59094.48 27675.59 FALSE # 2 B 45729.986 b 139889.21 111309.64 FALSE # 3 C 639686.154 c 1764684.16 1117027.29 TRUE # 4 D 4466.153 d 26250.32 20320.08 NA </code></pre> <p>@Jilber's solution won't work in that case and will lead to lots of misplaced columns. Instead, use <code>rbind.fill</code> from "plyr". I've used <code>sapply</code> to specify my function in this example to show that it is easy to use whatever function you want, not just the <code>col*</code> functions.</p> <pre><code>library(plyr) useme &lt;- sapply(mydf, is.numeric) rbind.fill(mydf, data.frame(t(sapply(mydf[useme], sum)))) # Description timetE4_1 Boo timetE1_2 timetE2_2 Baa # 1 A 4048.605 a 59094.48 27675.59 FALSE # 2 B 45729.986 b 139889.21 111309.64 FALSE # 3 C 639686.154 c 1764684.16 1117027.29 TRUE # 4 D 4466.153 d 26250.32 20320.08 NA # 5 &lt;NA&gt; 693930.898 &lt;NA&gt; 1989918.17 1276332.60 NA </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload