Thank you for posting! Here is an outline of an effective post:
Hello colleagues,
-
I need your help to modify a script where I can create a new variable (for different districts by using a specific formula for each district).
-
In my case, part of the formula for the new variable can remain the same but partly new for each district (i defined it as βerror_loadβ).
-
If I create this variable for each district step by step, one overwrite the previous one and the size of data remains equal to the size of the district in filter.
-
Can you please help me modify the code!
A help ideally within this week would be appreciated. Thank you!
``` r
demo_data <- data.frame(stringsAsFactors = FALSE,
Location_district = c("District1" , "District1" , "District3" , "District2",
"District1" , "District1" , "District2" , "District2",
"District3" , "District3"),
Test_A = c(0, 0, 5.14, 0 , 5.14 , 123.42 , 0 , 30.85, 15.42, 0),
Test_B = c(2.0571, 3.089, 5.14, 5.14, 5.14, 82.285, 3.08, 15.42, 5.14, 5.144),
Estimates = c(5,5,7,10,12, 3,6,6,3,6)
)
# Creating error_loads to be used in the formula for the "new variable" creation below
error_load_district1 <- 0.8
error_load_district2 <- 0.77
error_load_district3 <- 0.28
demo_data %>%
filter(Location_district == "District1") %>%
mutate(
Test_A_correction = Test_A * (0.14),
Test_B_correction = Test_B * (0.34),,
New_variable = (Estimates) * (0.88) * (error_load_district1) # creating new variable and # Use a different error_load based on the "filter" above
) %>%
filter(Location_district == "District2") %>%
mutate(
Test_A_correction = Test_A * (0.14),
Test_B_correction = Test_B * (0.34),,
New_variable = (Estimates) * (0.88) * (error_load_district2) # creating new variable and # Use a different error_load based on the "filter" above
) %>%
filter(Location_district == "District3") %>%
mutate(
Test_A_correction = Test_A * (0.14),
Test_B_correction = Test_B * (0.34),,
New_variable = (Estimates) * (0.88) * (error_load_district3) # creating new variable and # Use a different error_load based on the "filter" above
)
Created on 2024-08-14 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> setting value
#> version R version 4.3.1 (2023-06-16 ucrt)
#> os Windows 10 x64 (build 19045)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate Norwegian BokmΓ₯l_Norway.utf8
#> ctype Norwegian BokmΓ₯l_Norway.utf8
#> tz Europe/Oslo
#> date 2024-08-14
#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> package * version date (UTC) lib source
#> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.1)
#> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.1)
#> evaluate 0.22 2023-09-29 [1] CRAN (R 4.3.1)
#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1)
#> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.1)
#> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.3.1)
#> knitr 1.43 2023-05-25 [1] CRAN (R 4.3.1)
#> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.1)
#> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.1)
#> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.1)
#> rmarkdown 2.27 2024-05-17 [1] CRAN (R 4.3.3)
#> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1)
#> withr 2.5.1 2023-09-26 [1] CRAN (R 4.3.1)
#> xfun 0.39 2023-04-20 [1] CRAN (R 4.3.1)
#> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0)
#>
#> [1] C:/Program Files/R/library
#> [2] C:/Program Files/R/R-4.3.1/library
#>
#> ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ