Unable to get the sum in every section

xi <- c(3:5,8,8,8,9:11,14)
yi <- c(5,4,5,4,7,8,8,9,10,10)

xiyi <- xi*yi

xisq <- xi^2

yisq <- yi^2

cSum <- rowSums(xi)
fg <- data.frame(xi, yi, xiyi, xisq, yisq)

View(fg)

Trying to get the total sum in every column but unable to get it. Can somebody help me to get the result? Above is the code. Thankyou in advance.

image

Here's one way:

xi <- c(3:5, 8, 8, 8, 9:11, 14)
yi <- c(5, 4, 5, 4, 7, 8, 8, 9, 10, 10)

xiyi <- xi * yi
xisq <- xi^2
yisq <- yi^2

fg <- data.frame(xi, yi, xiyi, xisq, yisq)
fg["Total", ] <- colSums(x = fg)

fg
#>       xi yi xiyi xisq yisq
#> 1      3  5   15    9   25
#> 2      4  4   16   16   16
#> 3      5  5   25   25   25
#> 4      8  4   32   64   16
#> 5      8  7   56   64   49
#> 6      8  8   64   64   64
#> 7      9  8   72   81   64
#> 8     10  9   90  100   81
#> 9     11 10  110  121  100
#> 10    14 10  140  196  100
#> Total 80 70  620  740  540

Created on 2019-11-02 by the reprex package (v0.3.0)

1 Like

If your ultimate goal is to produce a table that you'll put in some report, then I can recommend looking into janitor package - https://github.com/sfirke/janitor#adorning-tabyls.

2 Likes