Conditional Mean Similar to AVERAGEIFS in ExcelSummarize with conditions in dplyrCalculate group mean while excluding current observation using dplyrGraphing percent of whole based on multiple criteriaHow to plot weighted means by group?R - Using data.table to efficiently test rolling conditions across multiple rows and columnsCustomizing points in RConditional filtering of data.frame with preceeding and tailing NA observationsLooping over multiple columns in a dataframe in RR coding: How to keep records with only 4 complete quarters of data and how to take a conditional sum with multiple conditionsR coding: How to take a conditional sum/mean with multiple conditions in a dataframeComparing Strings for Similarity - Based on Word ContentsHow to group or subset a data frame by two conditions in R
Can a network vulnerability be exploited locally?
What to do about my 1-month-old boy peeing through diapers?
How did medieval manors handle population growth? Were there room for more fields to be ploughed?
What does GDPR mean to myself regarding my own data?
Normalized Malbolge to Malbolge translator
The meaning of asynchronous vs synchronous
Was the six engine Boeing-747 ever seriously considered by Boeing?
Why can't you say don't instead of won't?
Coupling two 15 Amp circuit breaker for 20 Amp
Why is there no Disney logo in MCU movies?
What's the point of fighting monsters in Zelda BotW?
Why does AM radio react to IR remote?
Drawing probabilities on a simplex in TikZ
Why does glibc's strlen need to be so complicated to run quickly?
Is it unusual for a math department not to have a mail/web server?
How to handle inventory and story of a player leaving
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Why does `buck` mean `step-down`?
Get contents before a colon
Can I lend a small amount of my own money to a bank at the federal funds rate?
Heat output from a 200W electric radiator?
Is the internet in Madagascar faster than in UK?
Are spot colors limited and why CMYK mix is not treated same as spot color mix?
Why is 3/4 a simple meter while 6/8 is a compound meter?
Conditional Mean Similar to AVERAGEIFS in Excel
Summarize with conditions in dplyrCalculate group mean while excluding current observation using dplyrGraphing percent of whole based on multiple criteriaHow to plot weighted means by group?R - Using data.table to efficiently test rolling conditions across multiple rows and columnsCustomizing points in RConditional filtering of data.frame with preceeding and tailing NA observationsLooping over multiple columns in a dataframe in RR coding: How to keep records with only 4 complete quarters of data and how to take a conditional sum with multiple conditionsR coding: How to take a conditional sum/mean with multiple conditions in a dataframeComparing Strings for Similarity - Based on Word ContentsHow to group or subset a data frame by two conditions in R
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to write code that functions similar to that of an AVERAGEIFS function in Excel. Essentially, I would like to take the average of a variable given certain conditions are met. I have not been successful in finding a similar problem online given my situation calls for a unique condition that excludes certain instances.
What I am trying to accomplish is calculating the mean of total revenue (TOTALREV) for each vessel ID (VESSEL_ID) in all survey years (SURVEY_YEAR)--Excluding the current survey year. I have tried creating an additional column called YEAR to create a conditional "not equal" as well as what my code currently depicts.
CPTWG <- CEEDCdatEXP[,c("VESSEL_ID","SURVEY_YEAR","TOTALREV")] %>%
group_by(VESSEL_ID) %>%
summarize(AVERAGEREV = mean(TOTALREV[SURVEY_YEAR != SURVEY_YEAR]))
This code should create my ideal outcome:
VESSEL_ID <- c(1,2,3,1,2,3,1,2,3)
SURVEY_YEAR <- c(2009,2009,2009,2010,2010,2010,2011,2011,2011)
TOTALREV <- c(2718,9939,4014,7019,2016,2025,3218,7727,7252)
AVERAGEREV <- c(5118.5,4871.5,4638.5,2968,8833,5633,4868.5,5977.5,3019.5)
mydata <- data.frame(VESSEL_ID,SURVEY_YEAR,TOTALREV,AVERAGEREV)
r
add a comment |
I would like to write code that functions similar to that of an AVERAGEIFS function in Excel. Essentially, I would like to take the average of a variable given certain conditions are met. I have not been successful in finding a similar problem online given my situation calls for a unique condition that excludes certain instances.
What I am trying to accomplish is calculating the mean of total revenue (TOTALREV) for each vessel ID (VESSEL_ID) in all survey years (SURVEY_YEAR)--Excluding the current survey year. I have tried creating an additional column called YEAR to create a conditional "not equal" as well as what my code currently depicts.
CPTWG <- CEEDCdatEXP[,c("VESSEL_ID","SURVEY_YEAR","TOTALREV")] %>%
group_by(VESSEL_ID) %>%
summarize(AVERAGEREV = mean(TOTALREV[SURVEY_YEAR != SURVEY_YEAR]))
This code should create my ideal outcome:
VESSEL_ID <- c(1,2,3,1,2,3,1,2,3)
SURVEY_YEAR <- c(2009,2009,2009,2010,2010,2010,2011,2011,2011)
TOTALREV <- c(2718,9939,4014,7019,2016,2025,3218,7727,7252)
AVERAGEREV <- c(5118.5,4871.5,4638.5,2968,8833,5633,4868.5,5977.5,3019.5)
mydata <- data.frame(VESSEL_ID,SURVEY_YEAR,TOTALREV,AVERAGEREV)
r
Extremely slick answer to this here. For your purposes...mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
I was going to say that you could add some kind of dummy column that would take onNA
when your outliers happen. Then you could change yoursum
to work on that column with anna.rm = TRUE
argument. A couple of other options (including one from the author ofdplyr
) are linked here. You would probably have to tweak then() - 1
part, too, since your would likely have a lower denominator in your average.
– Nick Criswell
Mar 27 at 23:05
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22
add a comment |
I would like to write code that functions similar to that of an AVERAGEIFS function in Excel. Essentially, I would like to take the average of a variable given certain conditions are met. I have not been successful in finding a similar problem online given my situation calls for a unique condition that excludes certain instances.
What I am trying to accomplish is calculating the mean of total revenue (TOTALREV) for each vessel ID (VESSEL_ID) in all survey years (SURVEY_YEAR)--Excluding the current survey year. I have tried creating an additional column called YEAR to create a conditional "not equal" as well as what my code currently depicts.
CPTWG <- CEEDCdatEXP[,c("VESSEL_ID","SURVEY_YEAR","TOTALREV")] %>%
group_by(VESSEL_ID) %>%
summarize(AVERAGEREV = mean(TOTALREV[SURVEY_YEAR != SURVEY_YEAR]))
This code should create my ideal outcome:
VESSEL_ID <- c(1,2,3,1,2,3,1,2,3)
SURVEY_YEAR <- c(2009,2009,2009,2010,2010,2010,2011,2011,2011)
TOTALREV <- c(2718,9939,4014,7019,2016,2025,3218,7727,7252)
AVERAGEREV <- c(5118.5,4871.5,4638.5,2968,8833,5633,4868.5,5977.5,3019.5)
mydata <- data.frame(VESSEL_ID,SURVEY_YEAR,TOTALREV,AVERAGEREV)
r
I would like to write code that functions similar to that of an AVERAGEIFS function in Excel. Essentially, I would like to take the average of a variable given certain conditions are met. I have not been successful in finding a similar problem online given my situation calls for a unique condition that excludes certain instances.
What I am trying to accomplish is calculating the mean of total revenue (TOTALREV) for each vessel ID (VESSEL_ID) in all survey years (SURVEY_YEAR)--Excluding the current survey year. I have tried creating an additional column called YEAR to create a conditional "not equal" as well as what my code currently depicts.
CPTWG <- CEEDCdatEXP[,c("VESSEL_ID","SURVEY_YEAR","TOTALREV")] %>%
group_by(VESSEL_ID) %>%
summarize(AVERAGEREV = mean(TOTALREV[SURVEY_YEAR != SURVEY_YEAR]))
This code should create my ideal outcome:
VESSEL_ID <- c(1,2,3,1,2,3,1,2,3)
SURVEY_YEAR <- c(2009,2009,2009,2010,2010,2010,2011,2011,2011)
TOTALREV <- c(2718,9939,4014,7019,2016,2025,3218,7727,7252)
AVERAGEREV <- c(5118.5,4871.5,4638.5,2968,8833,5633,4868.5,5977.5,3019.5)
mydata <- data.frame(VESSEL_ID,SURVEY_YEAR,TOTALREV,AVERAGEREV)
r
r
asked Mar 27 at 20:39
Gary EatonGary Eaton
111 bronze badge
111 bronze badge
Extremely slick answer to this here. For your purposes...mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
I was going to say that you could add some kind of dummy column that would take onNA
when your outliers happen. Then you could change yoursum
to work on that column with anna.rm = TRUE
argument. A couple of other options (including one from the author ofdplyr
) are linked here. You would probably have to tweak then() - 1
part, too, since your would likely have a lower denominator in your average.
– Nick Criswell
Mar 27 at 23:05
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22
add a comment |
Extremely slick answer to this here. For your purposes...mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
I was going to say that you could add some kind of dummy column that would take onNA
when your outliers happen. Then you could change yoursum
to work on that column with anna.rm = TRUE
argument. A couple of other options (including one from the author ofdplyr
) are linked here. You would probably have to tweak then() - 1
part, too, since your would likely have a lower denominator in your average.
– Nick Criswell
Mar 27 at 23:05
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22
Extremely slick answer to this here. For your purposes...
mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
Extremely slick answer to this here. For your purposes...
mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
I was going to say that you could add some kind of dummy column that would take on
NA
when your outliers happen. Then you could change your sum
to work on that column with an na.rm = TRUE
argument. A couple of other options (including one from the author of dplyr
) are linked here. You would probably have to tweak the n() - 1
part, too, since your would likely have a lower denominator in your average.– Nick Criswell
Mar 27 at 23:05
I was going to say that you could add some kind of dummy column that would take on
NA
when your outliers happen. Then you could change your sum
to work on that column with an na.rm = TRUE
argument. A couple of other options (including one from the author of dplyr
) are linked here. You would probably have to tweak the n() - 1
part, too, since your would likely have a lower denominator in your average.– Nick Criswell
Mar 27 at 23:05
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22
add a comment |
0
active
oldest
votes
Your Answer
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%2f55386067%2fconditional-mean-similar-to-averageifs-in-excel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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%2f55386067%2fconditional-mean-similar-to-averageifs-in-excel%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
Extremely slick answer to this here. For your purposes...
mydata <- group_by(VESSEL_ID) %>% mutate(AVERAGEREV = (sum(TOTALREV) - TOTALREV) / (n() - 1))
– Nick Criswell
Mar 27 at 21:04
@NickCriswell--This is great! Thank you for digging this up as I was unable to do... In addition, I thought this would take me home, but to make things slightly more complex, I have one more column flagging each observation as an outlier or not. Any suggestions on how I might alter the mutate to incorporate only those data where outlier == 0 ?
– Gary Eaton
Mar 27 at 22:13
I was going to say that you could add some kind of dummy column that would take on
NA
when your outliers happen. Then you could change yoursum
to work on that column with anna.rm = TRUE
argument. A couple of other options (including one from the author ofdplyr
) are linked here. You would probably have to tweak then() - 1
part, too, since your would likely have a lower denominator in your average.– Nick Criswell
Mar 27 at 23:05
Thanks @NickCriswell!
– Gary Eaton
Mar 27 at 23:22