Noob stuggling again: Calculating percentage share per row of two columns

Hello everybody and sorry already for posting (hopefully in the correct manner) beginner questions.
Regarding a dataset of total assets and total personnel costs, I would like to know if there is any possibility to calculate the respective percentage shares for each company by a row-wise command.

Thank you already for any kind of advice and support

data.frame(
stringsAsFactors = FALSE,
Company = c("A","B","C","D","E","F",
"G","H","I","J","K","L","M"),
TA2017 = c(1066500,100464,77618,1056665,
11604429,913002,841644,847831,4833966,3698848,
3193911,4501316,1357833),
TPC2017 = c(658187,254310,67826,473905,
5403636,735155,876751,529764,2104877,355106,807324,
671716,1038536),
Percentage = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)

Since each company has its own line, you can calculate the ratio of the two columns using the mutate() function of dplyr.

DF <- data.frame(
  stringsAsFactors = FALSE,
  Company = c("A","B","C","D","E","F",
              "G","H","I","J","K","L","M"),
  TA2017 = c(1066500,100464,77618,1056665,
             11604429,913002,841644,847831,4833966,3698848,
             3193911,4501316,1357833),
  TPC2017 = c(658187,254310,67826,473905,
              5403636,735155,876751,529764,2104877,355106,807324,
              671716,1038536),
  Percentage = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
DF <- DF |>  mutate(Percentage = TPC2017/TA2017)
DF
#>    Company   TA2017 TPC2017 Percentage
#> 1        A  1066500  658187 0.61714674
#> 2        B   100464  254310 2.53135452
#> 3        C    77618   67826 0.87384370
#> 4        D  1056665  473905 0.44849124
#> 5        E 11604429 5403636 0.46565290
#> 6        F   913002  735155 0.80520634
#> 7        G   841644  876751 1.04171241
#> 8        H   847831  529764 0.62484623
#> 9        I  4833966 2104877 0.43543480
#> 10       J  3698848  355106 0.09600449
#> 11       K  3193911  807324 0.25276972
#> 12       L  4501316  671716 0.14922658
#> 13       M  1357833 1038536 0.76484811

Created on 2023-02-28 with reprex v2.0.2

Thank you sooooo much for your immediate answer and support!!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.