Calculation of Geometric Mean of Data that includes NAsGeometric Mean: is there a built-in?Aggregate function in R with multiple function argumentsHow to join (merge) data frames (inner, outer, left, right)Geometric Mean: is there a built-in?Drop data frame columns by nameHow to skip NA when applying geometric-mean functioncalculating weighted geometric mean of a large list of numbersMATLAB: cumulative geometric meanGeometric mean filter with opencvcalculate the harmonic mean and geometric mean without exceeding double max value?Calculating geometric mean every 10 min using dplyr or aggregte functionOptimizing calculation for Weighted Geometric Mean of a big set of data using GPU
Is there a way to proportionalize fixed costs in a MILP?
Bringing Power Supplies on Plane?
How far did Gandalf and the Balrog drop from the bridge in Moria?
Causal Diagrams using Wolfram?
How to gracefully leave a company you helped start?
Telephone number in spoken words
Execution order of f1() + f2()*f3() expression and operator precedence in JLS
How do I call a 6-digit Australian phone number with a US-based mobile phone?
When was "Fredo" an insult to Italian-Americans?
What would it take to get a message to another star?
How come the Rambam forbids picking up money found in the street?
How would armour (and combat) change if the fighter didn't need to actually wear it?
Global BGP Routing only by only importing supernet prefixes
How can I find files in directories listed in a file?
"Mouth-breathing" as slang for stupidity
Scam? Phone call from "Department of Social Security" asking me to call back
Units of measurement, especially length, when body parts vary in size among races
Running code generated in realtime in JavaScript with eval()
Chunk + Enumerate a list of digits
Boss wants me to ignore a software API license
Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu
How would you translate this? バタコチーズライス
When did Bilbo and Frodo learn that Gandalf was a Maia?
How did Arecibo detect methane lakes on Titan, and image Saturn's rings?
Calculation of Geometric Mean of Data that includes NAs
Geometric Mean: is there a built-in?Aggregate function in R with multiple function argumentsHow to join (merge) data frames (inner, outer, left, right)Geometric Mean: is there a built-in?Drop data frame columns by nameHow to skip NA when applying geometric-mean functioncalculating weighted geometric mean of a large list of numbersMATLAB: cumulative geometric meanGeometric mean filter with opencvcalculate the harmonic mean and geometric mean without exceeding double max value?Calculating geometric mean every 10 min using dplyr or aggregte functionOptimizing calculation for Weighted Geometric Mean of a big set of data using GPU
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
EDIT: The problem was not within the geoMean function, but with a wrong use of aggregate(), as explained in the comments
I am trying to calculate the geometric mean of multiple measurements for several different species, which includes NAs. An example of my data looks like this:
species <- c("Ae", "Ae", "Ae", "Be", "Be")
phen <- c(2, NA, 3, 1, 2)
hveg <- c(NA, 15, 12, 60, 59)
df <- data.frame(species, phen, hveg)
When I try to calculate the geometric mean for the species Ae with the built-in function geoMean from the package EnvStats like this
library("EnvStats")
aggregate(df[, 3:3], list(df1$Sp), geoMean, na.rm=TRUE)
it works wonderful and skips the NAs to give me the geometric means per species.
Group.1 phen hveg
1 Ae 4.238536 50.555696
2 Be 1.414214 1.414214
When I do this with my large dataset, however, the function stumbles over NAs and returns NA as result even though there are e.g 10 numerical values and only one NA. This happens for example with the column SLA_mm2/mg.
My large data set looks like this:
> str(cut2trait1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 22 obs. of 19 variables:
$ Cut : chr "15_08" "15_08" "15_08" "15_08" ...
$ Block : num 1 1 1 1 1 1 1 1 1 1 ...
$ ID : num 451 512 431 531 591 432 551 393 511 452 ...
$ Plot : chr "1_1" "1_1" "1_1" "1_1" ...
$ Grazing : chr "n" "n" "n" "n" ...
$ Acro : chr "Leuc.vulg" "Dact.glom" "Cirs.arve" "Trif.prat" ...
$ Sp : chr "Lv" "Dg" "Ca" "Tp" ...
$ Label_neu : chr "Lv021" "Dg022" "Ca021" "Tp021" ...
$ PlantFunctionalType: chr "forb" "grass" "forb" "forb" ...
$ PlotClimate : chr "AC" "AC" "AC" "AC" ...
$ Season : chr "Aug" "Aug" "Aug" "Aug" ...
$ Year : num 2015 2015 2015 2015 2015 ...
$ Tiller : num 6 3 3 5 6 8 5 2 1 7 ...
$ Hveg : num 25 38 70 36 68 65 23 58 71 27 ...
$ Hrep : num 39 54 77 38 76 70 65 88 98 38 ...
$ Phen : num 8 8 7 8 8 7 6.5 8 8 8 ...
$ SPAD : num 40.7 42.4 48.7 43 31.3 ...
$ TDW_in_g : num 4.62 4.85 11.86 5.82 8.99 ...
$ SLA_mm2/mg : num 19.6 19.8 20.3 21.2 21.7 ...
and the result of my code
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
is (only the first two rows):
Group.1 Tiller Hveg Hrep Phen SPAD TDW_in_g SLA_mm2/mg
1 Ae 13.521721 73.43485 106.67933 NA 28.17698 1.2602475 NA
2 Be 8.944272 43.95452 72.31182 5.477226 20.08880 0.7266361 9.309672
Here, the geometric mean of SLA for Ae is NA, even though there are 9 numeric measurements and only one NA in the column used to calculate the geometric mean.
I tried to use the geometric mean function suggested here:
Geometric Mean: is there a built-in?
But instead of NAs, this returned the value 1.000 when used with my big dataset, which doesn't solve my problem.
So my question is: What is the difference between my example df and the big dataset that throws the geoMean function off the rails?
r geometric-mean
add a comment |
EDIT: The problem was not within the geoMean function, but with a wrong use of aggregate(), as explained in the comments
I am trying to calculate the geometric mean of multiple measurements for several different species, which includes NAs. An example of my data looks like this:
species <- c("Ae", "Ae", "Ae", "Be", "Be")
phen <- c(2, NA, 3, 1, 2)
hveg <- c(NA, 15, 12, 60, 59)
df <- data.frame(species, phen, hveg)
When I try to calculate the geometric mean for the species Ae with the built-in function geoMean from the package EnvStats like this
library("EnvStats")
aggregate(df[, 3:3], list(df1$Sp), geoMean, na.rm=TRUE)
it works wonderful and skips the NAs to give me the geometric means per species.
Group.1 phen hveg
1 Ae 4.238536 50.555696
2 Be 1.414214 1.414214
When I do this with my large dataset, however, the function stumbles over NAs and returns NA as result even though there are e.g 10 numerical values and only one NA. This happens for example with the column SLA_mm2/mg.
My large data set looks like this:
> str(cut2trait1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 22 obs. of 19 variables:
$ Cut : chr "15_08" "15_08" "15_08" "15_08" ...
$ Block : num 1 1 1 1 1 1 1 1 1 1 ...
$ ID : num 451 512 431 531 591 432 551 393 511 452 ...
$ Plot : chr "1_1" "1_1" "1_1" "1_1" ...
$ Grazing : chr "n" "n" "n" "n" ...
$ Acro : chr "Leuc.vulg" "Dact.glom" "Cirs.arve" "Trif.prat" ...
$ Sp : chr "Lv" "Dg" "Ca" "Tp" ...
$ Label_neu : chr "Lv021" "Dg022" "Ca021" "Tp021" ...
$ PlantFunctionalType: chr "forb" "grass" "forb" "forb" ...
$ PlotClimate : chr "AC" "AC" "AC" "AC" ...
$ Season : chr "Aug" "Aug" "Aug" "Aug" ...
$ Year : num 2015 2015 2015 2015 2015 ...
$ Tiller : num 6 3 3 5 6 8 5 2 1 7 ...
$ Hveg : num 25 38 70 36 68 65 23 58 71 27 ...
$ Hrep : num 39 54 77 38 76 70 65 88 98 38 ...
$ Phen : num 8 8 7 8 8 7 6.5 8 8 8 ...
$ SPAD : num 40.7 42.4 48.7 43 31.3 ...
$ TDW_in_g : num 4.62 4.85 11.86 5.82 8.99 ...
$ SLA_mm2/mg : num 19.6 19.8 20.3 21.2 21.7 ...
and the result of my code
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
is (only the first two rows):
Group.1 Tiller Hveg Hrep Phen SPAD TDW_in_g SLA_mm2/mg
1 Ae 13.521721 73.43485 106.67933 NA 28.17698 1.2602475 NA
2 Be 8.944272 43.95452 72.31182 5.477226 20.08880 0.7266361 9.309672
Here, the geometric mean of SLA for Ae is NA, even though there are 9 numeric measurements and only one NA in the column used to calculate the geometric mean.
I tried to use the geometric mean function suggested here:
Geometric Mean: is there a built-in?
But instead of NAs, this returned the value 1.000 when used with my big dataset, which doesn't solve my problem.
So my question is: What is the difference between my example df and the big dataset that throws the geoMean function off the rails?
r geometric-mean
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
I couldn't do that, which led me to the original mistake:gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argumentna.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted tona.rm=FALSE
.
– Hedera.Helix
Mar 27 at 11:09
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15
add a comment |
EDIT: The problem was not within the geoMean function, but with a wrong use of aggregate(), as explained in the comments
I am trying to calculate the geometric mean of multiple measurements for several different species, which includes NAs. An example of my data looks like this:
species <- c("Ae", "Ae", "Ae", "Be", "Be")
phen <- c(2, NA, 3, 1, 2)
hveg <- c(NA, 15, 12, 60, 59)
df <- data.frame(species, phen, hveg)
When I try to calculate the geometric mean for the species Ae with the built-in function geoMean from the package EnvStats like this
library("EnvStats")
aggregate(df[, 3:3], list(df1$Sp), geoMean, na.rm=TRUE)
it works wonderful and skips the NAs to give me the geometric means per species.
Group.1 phen hveg
1 Ae 4.238536 50.555696
2 Be 1.414214 1.414214
When I do this with my large dataset, however, the function stumbles over NAs and returns NA as result even though there are e.g 10 numerical values and only one NA. This happens for example with the column SLA_mm2/mg.
My large data set looks like this:
> str(cut2trait1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 22 obs. of 19 variables:
$ Cut : chr "15_08" "15_08" "15_08" "15_08" ...
$ Block : num 1 1 1 1 1 1 1 1 1 1 ...
$ ID : num 451 512 431 531 591 432 551 393 511 452 ...
$ Plot : chr "1_1" "1_1" "1_1" "1_1" ...
$ Grazing : chr "n" "n" "n" "n" ...
$ Acro : chr "Leuc.vulg" "Dact.glom" "Cirs.arve" "Trif.prat" ...
$ Sp : chr "Lv" "Dg" "Ca" "Tp" ...
$ Label_neu : chr "Lv021" "Dg022" "Ca021" "Tp021" ...
$ PlantFunctionalType: chr "forb" "grass" "forb" "forb" ...
$ PlotClimate : chr "AC" "AC" "AC" "AC" ...
$ Season : chr "Aug" "Aug" "Aug" "Aug" ...
$ Year : num 2015 2015 2015 2015 2015 ...
$ Tiller : num 6 3 3 5 6 8 5 2 1 7 ...
$ Hveg : num 25 38 70 36 68 65 23 58 71 27 ...
$ Hrep : num 39 54 77 38 76 70 65 88 98 38 ...
$ Phen : num 8 8 7 8 8 7 6.5 8 8 8 ...
$ SPAD : num 40.7 42.4 48.7 43 31.3 ...
$ TDW_in_g : num 4.62 4.85 11.86 5.82 8.99 ...
$ SLA_mm2/mg : num 19.6 19.8 20.3 21.2 21.7 ...
and the result of my code
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
is (only the first two rows):
Group.1 Tiller Hveg Hrep Phen SPAD TDW_in_g SLA_mm2/mg
1 Ae 13.521721 73.43485 106.67933 NA 28.17698 1.2602475 NA
2 Be 8.944272 43.95452 72.31182 5.477226 20.08880 0.7266361 9.309672
Here, the geometric mean of SLA for Ae is NA, even though there are 9 numeric measurements and only one NA in the column used to calculate the geometric mean.
I tried to use the geometric mean function suggested here:
Geometric Mean: is there a built-in?
But instead of NAs, this returned the value 1.000 when used with my big dataset, which doesn't solve my problem.
So my question is: What is the difference between my example df and the big dataset that throws the geoMean function off the rails?
r geometric-mean
EDIT: The problem was not within the geoMean function, but with a wrong use of aggregate(), as explained in the comments
I am trying to calculate the geometric mean of multiple measurements for several different species, which includes NAs. An example of my data looks like this:
species <- c("Ae", "Ae", "Ae", "Be", "Be")
phen <- c(2, NA, 3, 1, 2)
hveg <- c(NA, 15, 12, 60, 59)
df <- data.frame(species, phen, hveg)
When I try to calculate the geometric mean for the species Ae with the built-in function geoMean from the package EnvStats like this
library("EnvStats")
aggregate(df[, 3:3], list(df1$Sp), geoMean, na.rm=TRUE)
it works wonderful and skips the NAs to give me the geometric means per species.
Group.1 phen hveg
1 Ae 4.238536 50.555696
2 Be 1.414214 1.414214
When I do this with my large dataset, however, the function stumbles over NAs and returns NA as result even though there are e.g 10 numerical values and only one NA. This happens for example with the column SLA_mm2/mg.
My large data set looks like this:
> str(cut2trait1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 22 obs. of 19 variables:
$ Cut : chr "15_08" "15_08" "15_08" "15_08" ...
$ Block : num 1 1 1 1 1 1 1 1 1 1 ...
$ ID : num 451 512 431 531 591 432 551 393 511 452 ...
$ Plot : chr "1_1" "1_1" "1_1" "1_1" ...
$ Grazing : chr "n" "n" "n" "n" ...
$ Acro : chr "Leuc.vulg" "Dact.glom" "Cirs.arve" "Trif.prat" ...
$ Sp : chr "Lv" "Dg" "Ca" "Tp" ...
$ Label_neu : chr "Lv021" "Dg022" "Ca021" "Tp021" ...
$ PlantFunctionalType: chr "forb" "grass" "forb" "forb" ...
$ PlotClimate : chr "AC" "AC" "AC" "AC" ...
$ Season : chr "Aug" "Aug" "Aug" "Aug" ...
$ Year : num 2015 2015 2015 2015 2015 ...
$ Tiller : num 6 3 3 5 6 8 5 2 1 7 ...
$ Hveg : num 25 38 70 36 68 65 23 58 71 27 ...
$ Hrep : num 39 54 77 38 76 70 65 88 98 38 ...
$ Phen : num 8 8 7 8 8 7 6.5 8 8 8 ...
$ SPAD : num 40.7 42.4 48.7 43 31.3 ...
$ TDW_in_g : num 4.62 4.85 11.86 5.82 8.99 ...
$ SLA_mm2/mg : num 19.6 19.8 20.3 21.2 21.7 ...
and the result of my code
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
is (only the first two rows):
Group.1 Tiller Hveg Hrep Phen SPAD TDW_in_g SLA_mm2/mg
1 Ae 13.521721 73.43485 106.67933 NA 28.17698 1.2602475 NA
2 Be 8.944272 43.95452 72.31182 5.477226 20.08880 0.7266361 9.309672
Here, the geometric mean of SLA for Ae is NA, even though there are 9 numeric measurements and only one NA in the column used to calculate the geometric mean.
I tried to use the geometric mean function suggested here:
Geometric Mean: is there a built-in?
But instead of NAs, this returned the value 1.000 when used with my big dataset, which doesn't solve my problem.
So my question is: What is the difference between my example df and the big dataset that throws the geoMean function off the rails?
r geometric-mean
r geometric-mean
edited Mar 27 at 11:21
Hedera.Helix
asked Mar 27 at 9:46
Hedera.HelixHedera.Helix
62 bronze badges
62 bronze badges
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
I couldn't do that, which led me to the original mistake:gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argumentna.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted tona.rm=FALSE
.
– Hedera.Helix
Mar 27 at 11:09
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15
add a comment |
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
I couldn't do that, which led me to the original mistake:gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argumentna.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted tona.rm=FALSE
.
– Hedera.Helix
Mar 27 at 11:09
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
I couldn't do that, which led me to the original mistake:
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argument na.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted to na.rm=FALSE
.– Hedera.Helix
Mar 27 at 11:09
I couldn't do that, which led me to the original mistake:
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argument na.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted to na.rm=FALSE
.– Hedera.Helix
Mar 27 at 11:09
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15
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%2f55374125%2fcalculation-of-geometric-mean-of-data-that-includes-nas%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%2f55374125%2fcalculation-of-geometric-mean-of-data-that-includes-nas%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
It is difficult to say what the difference is between the example dataset and your other dataset, for obvious reasons. Since you are computing the geometric mean per group, have you checked that there is at least one observation for each measurement in each group? Are all measurements positive?
– dipetkov
Mar 27 at 10:16
Thanks you for your suggestion. Yes, I checked for that and all observations are positive, and there's at least one for each group.
– Hedera.Helix
Mar 27 at 10:22
Then perhaps you can try to find a subset of your big dataset that still gives the error but it is small enough to post here?
– dipetkov
Mar 27 at 10:26
I couldn't do that, which led me to the original mistake:
gm_cut2trait1 <- aggregate(cut2trait1[, 13:19], list(cut2trait1$Sp), geoMean, na.rm=TRUE)
uses the argumentna.rm=TRUE
for the aggregate function, not for the geoMean function. Because of that, geoMean defaulted tona.rm=FALSE
.– Hedera.Helix
Mar 27 at 11:09
I found the answer to my problem (using an argument for a function within aggregate) here
– Hedera.Helix
Mar 27 at 11:15