Skip to content

Commit 8b1c05e

Browse files
committed
add documentation for @pivot_longer with all columns
1 parent 5233d52 commit 8b1c05e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

docs/examples/UserGuide/pivots.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ df_wide = DataFrame(id = [1, 2], A = [1, 3], B = [2, 4])
3636

3737
@pivot_longer(df_wide, -id)
3838

39+
# The selected columns can also be included as an array
40+
41+
@pivot_longer(df_wide, [id, B])
42+
43+
# or excluded
44+
45+
@pivot_longer(df_wide, -[id, B])
46+
47+
# If all columns should be included, they can be specified by either `everything()`, `:`, or by leaving the argument blank
48+
49+
@pivot_longer(df_wide, everything())
50+
3951
# 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.
4052

4153
# 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])
4557
# And here is an example with `names_to` and `values_to` containing bare unquoted variables:
4658

4759
@pivot_longer(df_wide, A:B, names_to = letter, values_to = number)
60+

test/test_pivots.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121
)
2222
test_long1 = @pivot_longer(test_df, -label)
2323
test_long2 = @pivot_longer(test_df, name:num)
24-
24+
2525
true_long3 = DataFrame(
2626
name = ["A","B","A","B"],
2727
num = [1,2,3,4],
@@ -37,21 +37,22 @@ end
3737
value = [1,1,2,2,1,2,3,4],
3838
)
3939
test_long5 = @pivot_longer(test_df, [label,num])
40-
40+
4141
true_long6 = DataFrame(
4242
label = [1,1,2,2],
4343
num = [1,2,3,4],
4444
variable = ["name","name","name","name"],
4545
value = ["A","B","A","B"],
4646
)
4747
test_long6 = @pivot_longer(test_df, -[label,num])
48-
48+
4949
true_long7 = DataFrame(
5050
variable = ["label","label","label","label","name","name","name","name","num","num","num","num"],
5151
value = [1,1,2,2,"A","B","A","B",1,2,3,4],
5252
)
5353
test_long7 = @pivot_longer(test_df, :)
5454
test_long8 = @pivot_longer(test_df)
55+
test_long9 = @pivot_longer(test_df, everything())
5556

5657
@test all(Array(true_long1 .== test_long1))
5758
@test all(Array(true_long1 .== test_long2))
@@ -61,5 +62,6 @@ end
6162
@test all(Array(true_long6 .== test_long6))
6263
@test all(Array(true_long7 .== test_long7))
6364
@test all(Array(true_long7 .== test_long8))
65+
@test all(Array(true_long7 .== test_long9))
6466
end
6567
end

0 commit comments

Comments
 (0)