Skip to content

Commit

Permalink
add documentation for @pivot_longer with all columns
Browse files Browse the repository at this point in the history
  • Loading branch information
cnrrobertson committed May 24, 2024
1 parent 5233d52 commit 8b1c05e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 13 additions & 0 deletions docs/examples/UserGuide/pivots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

8 changes: 5 additions & 3 deletions test/test_pivots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -37,21 +37,22 @@ 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],
variable = ["name","name","name","name"],
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))
Expand All @@ -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

0 comments on commit 8b1c05e

Please sign in to comment.