I have the below dataset:
Monday Tuesday Wednesday Friday Saturday Total
2 3 4 5 6 20
3 6 7 5 1 22
I am doing the below:
I need to divide first row: 2/20, 3/20, 4/20, 5/20, 6/20
And on the second row: 3/22, 6/22, 7/22, 5/22, 1/22
.
I can do this by extracting the columns but it is long and tedious, there must be an easier way.
To divide columns in Excel, just do the following: Divide two cells in the topmost row, for example: =A2/B2. Insert the formula in the first cell (say C2) and double-click the small green square in the lower-right corner of the cell to copy the formula down the column. Done!
Divide a column of numbers by a constant number In this example, the number you want to divide by is 3, contained in cell C2. Type =A2/$C$2 in cell B2. Be sure to include a $ symbol before C and before 2 in the formula. Drag the formula in B2 down to the other cells in column B.
On the Data tab, in the Data Tools group, click Text to Columns. The Convert Text to Columns Wizard opens. Choose Delimited if it is not already selected, and then click Next. Select the delimiter or delimiters to define the places where you want to split the cell content.
You can simply do
df[,1:5] / df[,6]
You can use dlyr
to operate rowwise on multiple columns:
library(tidyverse)
library(margrittr)
df <- data.frame(
Monday=c(2,3),
Tuesday=c(3,6),
Wednesday=c(4,7),
Friday=c(5,5),
Saturday=c(6,1),
Total=c(20,22))
df %>%
mutate(
across(c(1:5),
.fns = ~./Total))
This then returns:
Monday Tuesday Wednesday Friday Saturday Total
1 0.1000000 0.1500000 0.2000000 0.2500000 0.30000000 20
2 0.1363636 0.2727273 0.3181818 0.2272727 0.04545455 22
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With