From 8b1c05e2ec9232f4ea9ca4ee3688750ae4474139 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Fri, 24 May 2024 09:43:59 -0700 Subject: [PATCH] add documentation for @pivot_longer with all columns --- docs/examples/UserGuide/pivots.jl | 13 +++++++++++++ test/test_pivots.jl | 8 +++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/examples/UserGuide/pivots.jl b/docs/examples/UserGuide/pivots.jl index 76f1a56a..980a0782 100644 --- a/docs/examples/UserGuide/pivots.jl +++ b/docs/examples/UserGuide/pivots.jl @@ -36,6 +36,18 @@ df_wide = DataFrame(id = [1, 2], A = [1, 3], B = [2, 4]) @pivot_longer(df_wide, -id) +# The selected columns can also be included as an array + +@pivot_longer(df_wide, [id, B]) + +# or excluded + +@pivot_longer(df_wide, -[id, B]) + +# If all columns should be included, they can be specified by either `everything()`, `:`, or by leaving the argument blank + +@pivot_longer(df_wide, everything()) + # In this example, we set the `names_to` and `values_to` arguments. Either argument can be left out and will revert to the default value. The `names_to` and `values_to` arguments can be provided as strings or as bare unquoted variable names. # Here is an example with `names_to` and `values_to` containing strings: @@ -45,3 +57,4 @@ df_wide = DataFrame(id = [1, 2], A = [1, 3], B = [2, 4]) # And here is an example with `names_to` and `values_to` containing bare unquoted variables: @pivot_longer(df_wide, A:B, names_to = letter, values_to = number) + diff --git a/test/test_pivots.jl b/test/test_pivots.jl index 6a22b1ed..1ee4e451 100644 --- a/test/test_pivots.jl +++ b/test/test_pivots.jl @@ -21,7 +21,7 @@ end ) test_long1 = @pivot_longer(test_df, -label) test_long2 = @pivot_longer(test_df, name:num) - + true_long3 = DataFrame( name = ["A","B","A","B"], num = [1,2,3,4], @@ -37,7 +37,7 @@ end value = [1,1,2,2,1,2,3,4], ) test_long5 = @pivot_longer(test_df, [label,num]) - + true_long6 = DataFrame( label = [1,1,2,2], num = [1,2,3,4], @@ -45,13 +45,14 @@ end value = ["A","B","A","B"], ) test_long6 = @pivot_longer(test_df, -[label,num]) - + true_long7 = DataFrame( variable = ["label","label","label","label","name","name","name","name","num","num","num","num"], value = [1,1,2,2,"A","B","A","B",1,2,3,4], ) test_long7 = @pivot_longer(test_df, :) test_long8 = @pivot_longer(test_df) + test_long9 = @pivot_longer(test_df, everything()) @test all(Array(true_long1 .== test_long1)) @test all(Array(true_long1 .== test_long2)) @@ -61,5 +62,6 @@ end @test all(Array(true_long6 .== test_long6)) @test all(Array(true_long7 .== test_long7)) @test all(Array(true_long7 .== test_long8)) + @test all(Array(true_long7 .== test_long9)) end end