Mutate: Divide the variable by minimum value in a group

I would like to create a new variable by dividing the old variable by group means.

data <-
  data.frame(
    "Subgroup" = rep (1:2, c(10, 5)),
    "Observation" = c(rnorm(14), NA)
  )
data %>%
  group_by(Subgroup) %>%
  mutate("New variable" = Observation / mean(Observation, na.rm = TRUE),
                    GMean=mean(Observation, na.rm = TRUE))

I get the following output:

A tibble: 15 x 4

Groups: Subgroup [2]

Subgroup Observation New variable Gmean

1 1 0.185 -2.58 -0.0719
2 1 -0.0437 0.608 -0.0719
3 1 -0.216 3.00 -0.0719
4 1 1.46 -20.4 -0.0719
5 1 0.230 -3.19 -0.0719
6 1 0.108 -1.50 -0.0719
7 1 -1.38 19.2 -0.0719
8 1 -0.968 13.5 -0.0719
9 1 0.252 -3.50 -0.0719
10 1 -1.09 15.2 -0.0719
11 2 0.398 -5.53 -0.0719
12 2 -0.996 13.9 -0.0719
13 2 0.101 -1.40 -0.0719
14 2 0.954 -13.3 -0.0719
15 2 NA NA -0.0719

Since I have two groups I had expected to see the Gmean for group 1 and group 2 to be different but it appears the overal mean is used to divide. How can I get to divide bt the group mean?

I can't reproduce your issue with the code or data you provided.
Gmean is showing two values

You are right, I checked with R4.05 and R4.10 without using Rstudio and it is ok but when I run it via Rstudio I get a different result. My R sessionInfo from Rstudio is

sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets methods base

other attached packages:
[1] doseResponse_0.0.1.8 minpack.lm_1.2-1 emmeans_1.6.2-1 data.table_1.14.0 msm_1.6.8 plyr_1.8.6
[7] drc_3.2-0 drcData_1.1-3 MASS_7.3-54 nlme_3.1-152 plotly_4.9.4.1 yardstick_0.0.8
[13] workflowsets_0.1.0 workflows_0.2.3 tune_0.1.6 rsample_0.1.0 recipes_0.1.16 parsnip_0.1.7
[19] modeldata_0.1.1 infer_0.5.4 dials_0.0.9 scales_1.1.1 tidymodels_0.1.3 broom_0.7.9
[25] forcats_0.5.1 dplyr_1.0.7 purrr_0.3.4 readr_2.0.0 tidyr_1.1.3 tibble_3.1.3
[31] tidyverse_1.3.1 kableExtra_1.3.4 knitr_1.33 pander_0.6.4 qdapRegex_0.7.2 stringr_1.4.0
[37] readxl_1.3.1 egg_0.4.5 ggplot2_3.3.5 gridExtra_2.3 gtable_0.3.0

loaded via a namespace (and not attached):
[1] backports_1.2.1 systemfonts_1.0.2 lazyeval_0.2.2 splines_4.1.0 listenv_0.8.0 TH.data_1.0-10 digest_0.6.27
[8] foreach_1.5.1 htmltools_0.5.1.1 fansi_0.5.0 magrittr_2.0.1 tzdb_0.1.2 openxlsx_4.2.4 globals_0.14.0
[15] modelr_0.1.8 gower_0.2.2 sandwich_3.0-1 svglite_2.0.0 hardhat_0.1.6 colorspace_2.0-2 rvest_1.0.1
[22] haven_2.4.2 xfun_0.24 crayon_1.4.1 jsonlite_1.7.2 survival_3.2-11 zoo_1.8-9 iterators_1.0.13
[29] glue_1.4.2 ipred_0.9-11 webshot_0.5.2 car_3.0-11 abind_1.4-5 mvtnorm_1.1-2 DBI_1.1.1
[36] Rcpp_1.0.7 plotrix_3.8-1 xtable_1.8-4 viridisLite_0.4.0 GPfit_1.0-8 foreign_0.8-81 lava_1.6.9
[43] prodlim_2019.11.13 htmlwidgets_1.5.3 httr_1.4.2 ellipsis_0.3.2 farver_2.1.0 pkgconfig_2.0.3 nnet_7.3-16
[50] dbplyr_2.1.1 utf8_1.2.2 labeling_0.4.2 tidyselect_1.1.1 rlang_0.4.11 DiceDesign_1.9 munsell_0.5.0
[57] cellranger_1.1.0 tools_4.1.0 cli_3.0.1 generics_0.1.0 evaluate_0.14 yaml_2.2.1 fs_1.5.0
[64] zip_2.2.0 future_1.21.0 xml2_1.3.2 compiler_4.1.0 rstudioapi_0.13 curl_4.3.2 reprex_2.0.0
[71] lhs_1.1.1 stringi_1.7.3 highr_0.9 lattice_0.20-44 Matrix_1.3-4 vctrs_0.3.8 pillar_1.6.2
[78] lifecycle_1.0.0 furrr_0.2.3 estimability_1.3 R6_2.5.0 rio_0.5.27 parallelly_1.27.0 codetools_0.2-18
[85] gtools_3.9.2 assertthat_0.2.1 withr_2.4.2 multcomp_1.4-17 expm_0.999-6 parallel_4.1.0 hms_1.1.0
[92] rpart_4.1-15 timeDate_3043.102 coda_0.19-4 class_7.3-19 rmarkdown_2.9 carData_3.0-4 pROC_1.17.0.1
[99] lubridate_1.7.10

The first thing you should do is set.seed() on your script. That way you can make a plausible comparison between running in different environments. I.e Rstudio or Rterm or Rgui or what have you.

Also check your dplyr package version across the differing environment's.

Thank you for the suggestions.

Regards,

Luwis

This topic was automatically closed 21 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.