0

How do you multiply a number by a percentage increase in R. E.g.

43424 increasing by 120% would be 43424 * 2 + 43424 * 0.2

I have increases by 200% + and decreases in percentage as well

1 Answers1

1

The simple case:

increase    <- 1.20
start_value <- 43424
inc_value   <- start_value * (1 + increase)

If you don't want for some reason calculate the percentage, define a value without the %-sign

percentage  <- 120
increase    <- percentage/100
start_value <- 43424
inc_value   <- start_value * (1 + increase)

If you just have values with % you could transform them into numerical values

percentage  <- c("120 %", "-200%")
increase    <- as.numeric(gsub("[[:space:]]*%", "", percentage))/100
start_value <- 43424
inc_value   <- start_value * (1 + increase)

The regular expression uses removes all spaces and a following %. Hope this solves your issue.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • Thanks this is great, but what do I do if the start values is different, e.g. startvalue1 is 200 and startvalue2 is 300 but percentagechange1 is 200% and percentagechange2 is -50 – user123231322 May 17 '20 at 17:35
  • If your startvalues are given by `c(43242, 12345)` and your percentage change is given by `c("120 %", "150%")` then `start_value * (1 + increase)` gives you `43424*2.2` and `12345*2.5` at once. – Martin Gal May 17 '20 at 17:45
  • @user123231322 And if you are satisfied, feel free to accept the answer (https://stackoverflow.com/help/someone-answers). :-) – Martin Gal May 17 '20 at 18:23