1) proportions If your input is df1 (shown reproducibly in the Note at the end) then change the column names to those desired and convert it to a matrix m. Finally use proportions on that with a margin of 1 meaning row proportions -- 2 would mean column proportions. Note that we converted to a matrix in the first line because proportions requires that.
m <- as.matrix(setNames(df1[-1], c("%M", "%F")))
cbind(df1, 100 * proportions(m, 1))
## Industry Male Female %M %F
## 1 Art/Entertainment 100 500 16.666667 83.33333
## 2 Banking 600 100 85.714286 14.28571
## 3 Healthcare 53 65 44.915254 55.08475
## ...snip...
2) rowSums Another approach is to divide df1[-1] by the rowSums giving the same reuslt.
cbind(df1, setNames(100 * df1[-1] / rowSums(df1[-1]), c("%M", "%F")))
## Industry Male Female %M %F
## 1 Art/Entertainment 100 500 16.666667 83.33333
## 2 Banking 600 100 85.714286 14.28571
## 3 Healthcare 53 65 44.915254 55.08475
## ...snip...
3) dplyr Use across to make a copy of the columns with the indicated names and then multiply that by 100 and use c_across to divide by the sum of the columns
df1 %>%
group_by(Industry) %>%
mutate(100 * across(.names = "%{.col}") / sum(c_across())) %>%
ungroup
## # A tibble: 9 x 5
## Industry Male Female `%Male` `%Female`
## <chr> <int> <int> <dbl> <dbl>
## 1 Art/Entertainment 100 500 16.7 83.3
## 2 Banking 600 100 85.7 14.3
## 3 Healthcare 53 65 44.9 55.1
## ...snip...
4) transform This one is close to another answer but it does not overwrite the input:
transform(df1,
"%M" = 100 * Male / (Male + Female),
"%F" = 100 * Female / (Male + Female),
check.names = FALSE)
## Industry Male Female %M %F
## 1 Art/Entertainment 100 500 16.666667 83.33333
## 2 Banking 600 100 85.714286 14.28571
## 3 Healthcare 53 65 44.915254 55.08475
## ...snip...
Note
Input in reproducible form:
df1 <- structure(list(Industry = c("Art/Entertainment", "Banking", "Healthcare",
"Education", "Military", "Medicine", "Law", "Computer", "Sales"
), Male = c(100L, 600L, 53L, 20L, 47L, 500L, 500L, 200L, 420L
), Female = c(500L, 100L, 65L, 766L, 96L, 400L, 500L, 144L, 69L
)), class = "data.frame", row.names = c(NA, -9L))