## Dividing columns by colSums in R

I am trying to scale the values in a matrix so that each column adds up to one. I have tried:

m = matrix(c(1:9),nrow=3, ncol=3, byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 colSums(m) 12 15 18 m = m/colSums(m) [,1] [,2] [,3] [1,] 0.08333333 0.1666667 0.25 [2,] 0.26666667 0.3333333 0.40 [3,] 0.38888889 0.4444444 0.50 colSums(m) [1] 0.7388889 0.9444444 1.1500000

so obviously this doesn't work. I then tried this:

m = m/matrix(rep(colSums(m),3), nrow=3, ncol=3, byrow=T) [,1] [,2] [,3] [1,] 0.08333333 0.1333333 0.1666667 [2,] 0.33333333 0.3333333 0.3333333 [3,] 0.58333333 0.5333333 0.5000000 m = colSums(m) [1] 1 1 1

so this works, but it feels like I'm missing something here. This can't be how it is routinely done. I'm certain I am being stupid here. Any help you can give would be appreciated Cheers, Davy

See `?sweep`

, eg:

> sweep(m,2,colSums(m),`/`) [,1] [,2] [,3] [1,] 0.08333333 0.1333333 0.1666667 [2,] 0.33333333 0.3333333 0.3333333 [3,] 0.58333333 0.5333333 0.5000000

or you can transpose the matrix and then `colSums(m)`

gets recycled correctly. Don't forget to transpose afterwards again, like this :

> t(t(m)/colSums(m)) [,1] [,2] [,3] [1,] 0.08333333 0.1333333 0.1666667 [2,] 0.33333333 0.3333333 0.3333333 [3,] 0.58333333 0.5333333 0.5000000

Or you use the function `prop.table()`

to do basically the same:

> prop.table(m,2) [,1] [,2] [,3] [1,] 0.08333333 0.1333333 0.1666667 [2,] 0.33333333 0.3333333 0.3333333 [3,] 0.58333333 0.5333333 0.5000000

The time differences are rather small. the `sweep()`

function and the `t()`

trick are the most flexible solutions, `prop.table()`

is only for this particular case

**Sweeping vs. dividing,** Normalizing columns or rows of a data set is a very common operation. This is quick and easy in R by just dividing the matrix by its row sums: cs <- colSums(m); m/cs , you would get gibberish, without a warning: R would How can I divide all values of a given column by a number? Lets say I have a dataframe that contains 10 columns and I need to divide by 1000 one of them. I've tried a couple of solutions bot none of them works well.

Per usual, Joris has a great answer. Two others that came to mind:

#Essentially your answer f1 <- function() m / rep(colSums(m), each = nrow(m)) #Two calls to transpose f2 <- function() t(t(m) / colSums(m)) #Joris f3 <- function() sweep(m,2,colSums(m),`/`)

Joris' answer is the fastest on my machine:

> m <- matrix(rnorm(1e7), ncol = 10000) > library(rbenchmark) > benchmark(f1,f2,f3, replications=1e5, order = "relative") test replications elapsed relative user.self sys.self user.child sys.child 3 f3 100000 0.386 1.0000 0.385 0.001 0 0 1 f1 100000 0.421 1.0907 0.382 0.002 0 0 2 f2 100000 0.465 1.2047 0.386 0.003 0 0

**R help,** How do I divide each row/column by its rowSum/colSums and still return in the matrix form? (i.e. the new rowSums/colSums =1) Thanks. Casper. how to divide each column in a matrix by its colSums? Hi, I have a matrix, say m=matrix(c( 983,679,134, 383,416,84, 2892,2625,570 ),nrow=3 ) i can find its row/col sum by rowSums(m) colSums(m) How do I divide each row/column by its rowSum/colSums and still return in the matrix form?

Certainly late, but I just used the

adorn_percentages(table.with.value, denominator = "col").

More information on the following link : https://rdrr.io/cran/janitor/man/adorn_percentages.html

**colSums: Form Row and Column Sums and Means,** colSums() etc, a numeric, integer or logical matrix (or vector of length m * n ). na.rm. logical. Should missing values (including NaN ) be omitted from the Dividing columns by colSums in R. Ask Question Asked 8 years, 4 months ago. Active 1 month ago. Viewed 21k times 22. 5. I am trying to scale the values in a matrix so

**[R] how to divide each column in a matrix by its colSums?,** [R] how to divide each column in a matrix by its colSums? Spencer Graves spencer.graves at structuremonitoring.com. Sun Nov 28 22:44:12 One of the easiest tasks to perform in R is adding a new column to a data frame based on one or more other columns. we could add a column for profit margin by dividing profit by revenue and

**Column-wise matrix normalization in R,** Or you could construct the full matrix you want to divide by, like you did in your question. Here's another way you might do that. m/colSums(m)[col Another way to subset the data frame with brackets is by omitting row and column references. Take a look at this code: ed_exp2 - education[-c(1:9,22:50),-c(1,3:5)] Here, instead of subsetting the rows and columns we wanted returned, we subsetted the rows and columns we did not want returned and then omitted them with the “-” sign.

**Give column sums of a matrix or data frame, based on a grouping ,** Compute column sums across rows of a matrix-like object for each level of a col(x)), sum) t(sapply(split(as.data.frame(x), group), colSums)) aggregate(x, Very often you may have to manipulate a column of text in a data frame with R. You may want to separate a column in to multiple columns in a data frame or you may want to split a column of text and keep only a part of it. tidyr’s separate function is the best option to separate a column or split a column of text the way you want.