| Title: | Create Pivot Table Easily |
|---|---|
| Description: | Pivot easily by specifying rows, columns, values and split. |
| Authors: | Toshikazu Matsumura [aut, cre] (ORCID: <https://orcid.org/0000-0003-2306-6355>) |
| Maintainer: | Toshikazu Matsumura <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.2.9000 |
| Built: | 2026-05-26 06:38:34 UTC |
| Source: | https://github.com/matutosi/pivotea |
Add sub index within group
add_group_sub(df, group, sep = "_", tmp_col = "tmp_col")add_group_sub(df, group, sep = "_", tmp_col = "tmp_col")
df |
A dataframe. |
group |
A string or string vector. When vector, the first string will be used for adding sub index. |
sep |
A string for separator. |
tmp_col |
A string of colnames for temporary use. |
A dataframe.
library(dplyr) add_group_sub(mtcars, c("am", "gear")) add_group_sub(mtcars, c("cyl", "am"))library(dplyr) add_group_sub(mtcars, c("am", "gear")) add_group_sub(mtcars, c("cyl", "am"))
Helper for na_col_omit()
extract_col(col, df)extract_col(col, df)
col |
A string or string vector. |
df |
A dataframe. |
A vector.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")
Detect if df has col
has_col(df, col)has_col(df, col)
df |
A dataframe. |
col |
A string or string vector. |
A dataframe.
colnames(mtcars) has_col(mtcars, c("mpg", "cyl")) has_col(mtcars, c("mpg", "foo"))colnames(mtcars) has_col(mtcars, c("mpg", "cyl")) has_col(mtcars, c("mpg", "foo"))
Timetable in Hogwarts School of Witchcraft and Wizardry.
hogwartshogwarts
A data frame with 548 rows and 7 variable:
Grades in school.
Houses. G: Gryffindor, S: Slytherin, R: Ravenclaw, and H: Hufflepuff.
Abbreviations of day of the week.
Hours.
Teachers.
Subjects.
data(hogwarts) hogwartsdata(hogwarts) hogwarts
replace NA character into ""
na2empty(df)na2empty(df)
df |
A dataframe. |
A dataframe.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")
Remove all NA cols
omit_na_cols(df)omit_na_cols(df)
df |
A dataframe. |
A dataframe.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")
Remove all NA rows
omit_na_rows(df)omit_na_rows(df)
df |
A dataframe. |
A dataframe.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")
Pivot easily by specifying rows, columns, values and split.
pivot(df, row, col, value, split = NULL, sep = "_", rm_empty_df = TRUE)pivot(df, row, col, value, split = NULL, sep = "_", rm_empty_df = TRUE)
df |
A dataframe. |
row, value
|
A string or string vector. |
col |
A string or string vector. |
split |
A string or string vector. |
sep |
A string for separator. |
rm_empty_df |
A logical for removing empty df. |
A dataframe.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")
Replace a col with a data.frame.
replace_col(df, replace)replace_col(df, replace)
df, replace
|
A dataframe. |
A dataframe.
(state <- tibble::tibble(state = state.name, area = state.area)) (abb <- tibble::tibble(state = state.name, abb = state.abb)) replace_col(state, abb)(state <- tibble::tibble(state = state.name, area = state.area)) (abb <- tibble::tibble(state = state.name, abb = state.abb)) replace_col(state, abb)
Split by force with "" when split is NULL
split_force(df, split)split_force(df, split)
df |
A dataframe. |
split |
A string or string vector. |
A dataframe.
split_force(mtcars, split = NULL) split_force(mtcars, split = c("cyl"))split_force(mtcars, split = NULL) split_force(mtcars, split = c("cyl"))
Validate col
validate_col(df, col)validate_col(df, col)
df |
A dataframe. |
col |
A string or string vector. |
A dataframe.
library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")library(tidyr) library(dplyr) library(purrr) library(ggplot2) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "teacher", "room"), split = c("house", "grade")) hogwarts |> pivot(row = "hour", col = "wday", value = c("subject", "room", "house", "grade"), split = c("teacher")) starwars |> pivot(row = "homeworld", col = "species", value = "name", split = "sex") msleep |> pivot(row = "vore", col = "conservation", value = "name") |> na2empty() |> print(n = Inf) tibble::as_tibble(Titanic) |> pivot(row = "Age", col = c("Sex", "Survived"), value = "n", split = "Class") diamonds |> pivot(row = "cut", col = "color", value = "price", split = "clarity")