Find cumulative number by increment in percentage in R
I have a dataframe (new_df) as below ( sample of original)
structure(list(Facility = c("ABE", "ABE", "ABE", "ABE", "ABE",
"ABE", "ABE", "ABE", "ABE", "ABE"), Year.x = c(2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025, 2026), `Growth Rate` = c(25.375,
15.5865736124958, 14.5677592486103, 12.3473314371758, 10.253164556962,
6.67251975417032, 2.65906932573599, 1.55411655874191, 1.51211513936965,
1.5853074898301), Year.y = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), OPS = c(7200, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I want to input value in OPS such that it shows cumulative times percent increment (column Growth rate)
Below is gist of work I am trying to do
For (all facilities in OPSNET)
For each facility in OPSNET find the new number of operations for year 2017 to 2045 which is based on the growth rate of each facility for each year. Remember that growth rate is give in “GR”.
Let’s say you want to do that for facility “ABE”. First you multiply 7200 by growth rate in year 2017 (25) and sum it up with 7200 which gives you 9027. Then you multiply 9027 by growth rate in 2018 (15) and sum it up with 9027 which gives you 10434. You do that until year 2045.
I have tried below using dplyr but I am struggling how to multiple with percent increment while taking
my take :-
new_df <- new_df %>% arrange(Facility, Year.x) %>%
mutate(cumsum = cumsum(OPS))
Please assist me!
r dplyr tidyverse cumulative-sum
add a comment |
I have a dataframe (new_df) as below ( sample of original)
structure(list(Facility = c("ABE", "ABE", "ABE", "ABE", "ABE",
"ABE", "ABE", "ABE", "ABE", "ABE"), Year.x = c(2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025, 2026), `Growth Rate` = c(25.375,
15.5865736124958, 14.5677592486103, 12.3473314371758, 10.253164556962,
6.67251975417032, 2.65906932573599, 1.55411655874191, 1.51211513936965,
1.5853074898301), Year.y = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), OPS = c(7200, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I want to input value in OPS such that it shows cumulative times percent increment (column Growth rate)
Below is gist of work I am trying to do
For (all facilities in OPSNET)
For each facility in OPSNET find the new number of operations for year 2017 to 2045 which is based on the growth rate of each facility for each year. Remember that growth rate is give in “GR”.
Let’s say you want to do that for facility “ABE”. First you multiply 7200 by growth rate in year 2017 (25) and sum it up with 7200 which gives you 9027. Then you multiply 9027 by growth rate in 2018 (15) and sum it up with 9027 which gives you 10434. You do that until year 2045.
I have tried below using dplyr but I am struggling how to multiple with percent increment while taking
my take :-
new_df <- new_df %>% arrange(Facility, Year.x) %>%
mutate(cumsum = cumsum(OPS))
Please assist me!
r dplyr tidyverse cumulative-sum
Usecumprod
. For instance,cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initialOPS
value.
– nicola
Nov 23 '18 at 12:03
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52
add a comment |
I have a dataframe (new_df) as below ( sample of original)
structure(list(Facility = c("ABE", "ABE", "ABE", "ABE", "ABE",
"ABE", "ABE", "ABE", "ABE", "ABE"), Year.x = c(2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025, 2026), `Growth Rate` = c(25.375,
15.5865736124958, 14.5677592486103, 12.3473314371758, 10.253164556962,
6.67251975417032, 2.65906932573599, 1.55411655874191, 1.51211513936965,
1.5853074898301), Year.y = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), OPS = c(7200, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I want to input value in OPS such that it shows cumulative times percent increment (column Growth rate)
Below is gist of work I am trying to do
For (all facilities in OPSNET)
For each facility in OPSNET find the new number of operations for year 2017 to 2045 which is based on the growth rate of each facility for each year. Remember that growth rate is give in “GR”.
Let’s say you want to do that for facility “ABE”. First you multiply 7200 by growth rate in year 2017 (25) and sum it up with 7200 which gives you 9027. Then you multiply 9027 by growth rate in 2018 (15) and sum it up with 9027 which gives you 10434. You do that until year 2045.
I have tried below using dplyr but I am struggling how to multiple with percent increment while taking
my take :-
new_df <- new_df %>% arrange(Facility, Year.x) %>%
mutate(cumsum = cumsum(OPS))
Please assist me!
r dplyr tidyverse cumulative-sum
I have a dataframe (new_df) as below ( sample of original)
structure(list(Facility = c("ABE", "ABE", "ABE", "ABE", "ABE",
"ABE", "ABE", "ABE", "ABE", "ABE"), Year.x = c(2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025, 2026), `Growth Rate` = c(25.375,
15.5865736124958, 14.5677592486103, 12.3473314371758, 10.253164556962,
6.67251975417032, 2.65906932573599, 1.55411655874191, 1.51211513936965,
1.5853074898301), Year.y = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), OPS = c(7200, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I want to input value in OPS such that it shows cumulative times percent increment (column Growth rate)
Below is gist of work I am trying to do
For (all facilities in OPSNET)
For each facility in OPSNET find the new number of operations for year 2017 to 2045 which is based on the growth rate of each facility for each year. Remember that growth rate is give in “GR”.
Let’s say you want to do that for facility “ABE”. First you multiply 7200 by growth rate in year 2017 (25) and sum it up with 7200 which gives you 9027. Then you multiply 9027 by growth rate in 2018 (15) and sum it up with 9027 which gives you 10434. You do that until year 2045.
I have tried below using dplyr but I am struggling how to multiple with percent increment while taking
my take :-
new_df <- new_df %>% arrange(Facility, Year.x) %>%
mutate(cumsum = cumsum(OPS))
Please assist me!
r dplyr tidyverse cumulative-sum
r dplyr tidyverse cumulative-sum
edited Nov 23 '18 at 11:58
Rui Barradas
18.2k51833
18.2k51833
asked Nov 23 '18 at 11:53
Vaibhav SinghVaibhav Singh
8112
8112
Usecumprod
. For instance,cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initialOPS
value.
– nicola
Nov 23 '18 at 12:03
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52
add a comment |
Usecumprod
. For instance,cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initialOPS
value.
– nicola
Nov 23 '18 at 12:03
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52
Use
cumprod
. For instance, cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initial OPS
value.– nicola
Nov 23 '18 at 12:03
Use
cumprod
. For instance, cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initial OPS
value.– nicola
Nov 23 '18 at 12:03
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52
add a comment |
1 Answer
1
active
oldest
votes
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446239%2ffind-cumulative-number-by-increment-in-percentage-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
add a comment |
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
add a comment |
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
answered Nov 23 '18 at 14:12
BramBram
1299
1299
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446239%2ffind-cumulative-number-by-increment-in-percentage-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use
cumprod
. For instance,cumprod(1+df$"Growth Rate"/100)
gives you the cumulative rates you need to multiply to the initialOPS
value.– nicola
Nov 23 '18 at 12:03
Thanks for quick help but how would cumprod be used for each levels in facility column, could you please clear that with an example. I feel its something very little just could not figure it out
– Vaibhav Singh
Nov 23 '18 at 12:07
Can you post the expected result? Not sure to really understand what you need
– Bram
Nov 23 '18 at 13:52