• R/O
  • SSH

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqt誰得cocoawindowspythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdombtronvb.netdirectxarduinopreviewerゲームエンジン

Commit MetaInfo

Revision21f423fc04aa2accb2d5581ba62db91007a400df (tree)
Zeit2022-06-30 09:39:17
AutorLorenzo Isella <lorenzo.isella@gmai...>
CommiterLorenzo Isella

Log Message

I converted another function from %>% to |>.

Ändern Zusammenfassung

Diff

diff -r 7b4c16c8fccd -r 21f423fc04aa R-codes/stat_lib.R
--- a/R-codes/stat_lib.R Thu Jun 30 01:56:43 2022 +0200
+++ b/R-codes/stat_lib.R Thu Jun 30 02:39:17 2022 +0200
@@ -5632,49 +5632,80 @@
56325632 ### adding totals along the years. It also relies on the curly-curly syntax.
56335633
56345634
5635+
5636+
5637+## make_wide_with_total <- function(x, var1_name, var2_quantity, var_out="All years",
5638+## total_name="Total",pos="bottom", ...){
5639+
5640+## if (pos=="bottom"){
5641+
5642+## res <- x %>%
5643+
5644+## pivot_wider(names_from=all_of(var1_name),
5645+## values_from=all_of(var2_quantity),
5646+## names_sort = T ) %>%
5647+## na_to_pattern(0) %>%
5648+## add_total(nrow(.)+1, name = total_name, ...) %>%
5649+## rowwise() %>%
5650+## mutate({{var_out}}:=sum(c_across(where(is.numeric)))) %>%
5651+## ungroup()
5652+## } else if (pos=="top"){
5653+
5654+
5655+## res <- x %>%
5656+## pivot_wider(names_from=all_of(var1_name),
5657+## values_from=all_of(var2_quantity),
5658+## names_sort = T ) %>%
5659+## na_to_pattern(0) %>%
5660+## add_total(1, name = total_name, ...) %>%
5661+## rowwise() %>%
5662+## mutate({{var_out}}:=sum(c_across(where(is.numeric)))) %>%
5663+## ungroup()
5664+## }
5665+
5666+## return(res)
5667+## }
5668+
5669+
5670+## I rewrite the function above using the native pipe.
5671+
56355672 make_wide_with_total <- function(x, var1_name, var2_quantity, var_out="All years",
56365673 total_name="Total",pos="bottom", ...){
56375674
56385675 if (pos=="bottom"){
56395676
5640- res <- x |> ## %>%
5641- ## mutate(var1=as.character(var1)) %>%
5677+ res <- x |>
56425678
56435679 pivot_wider(names_from=all_of(var1_name),
56445680 values_from=all_of(var2_quantity),
5645- names_sort = T ) |> ## %>%
5646- na_to_pattern(0) |> ## %>%
5647- ## round_all(round_num) %>%
5648- add_total(nrow(.)+1, name = total_name, ...) |> ## %>%
5649- rowwise() |> ## %>%
5650- mutate({{var_out}}:=sum(c_across(where(is.numeric)))) |> ## %>%
5681+ names_sort = T ) |>
5682+ na_to_pattern(0) |>
5683+ ## add_total(nrow(.)+1, name = total_name, ...) |>
5684+ (\(x) add_total(x, nrow(x)+1, name=total_name, ...))() |>
5685+ rowwise() |>
5686+ mutate({{var_out}}:=sum(c_across(where(is.numeric)))) |>
56515687 ungroup()
56525688 } else if (pos=="top"){
56535689
56545690
5655- res <- x |> ## %>%
5656- ## mutate(var1=as.character(var1)) %>%
5657-
5691+ res <- x %>%
56585692 pivot_wider(names_from=all_of(var1_name),
56595693 values_from=all_of(var2_quantity),
5660- names_sort = T ) |> ## %>%
5661- na_to_pattern(0) |> ## %>%
5662- ## round_all(round_num) %>%
5663- add_total(1, name = total_name, ...) |> ## %>%
5664- rowwise() |> ## %>%
5665- mutate({{var_out}}:=sum(c_across(where(is.numeric)))) |> ## %>%
5694+ names_sort = T ) |>
5695+ na_to_pattern(0) |>
5696+ add_total(1, name = total_name, ...) |>
5697+ rowwise() |>
5698+ mutate({{var_out}}:=sum(c_across(where(is.numeric)))) |>
56665699 ungroup()
5667-
5668-
5669-}
5670-
5671-
5672-
5700+}
5701+
56735702 return(res)
56745703 }
56755704
56765705
56775706
5707+
5708+
56785709 ## a function to calculate the total on grouped data
56795710
56805711 ## See https://stackoverflow.com/questions/61178470/dplyr-group-modify-and-anonymous-functions/61178540#61178540