Create a pivot table

pivotr(
  dataset,
  cvars = "",
  nvar = "None",
  fun = "mean",
  normalize = "None",
  tabfilt = "",
  tabsort = "",
  nr = Inf,
  data_filter = "",
  envir = parent.frame()
)

Arguments

dataset

Dataset to tabulate

cvars

Categorical variables

nvar

Numerical variable

fun

Function to apply to numerical variable

normalize

Normalize the table by row total, column totals, or overall total

tabfilt

Expression used to filter the table (e.g., "Total > 10000")

tabsort

Expression used to sort the table (e.g., "desc(Total)")

nr

Number of rows to display

data_filter

Expression used to filter the dataset before creating the table (e.g., "price > 10000")

envir

Environment to extract data from

Details

Create a pivot-table. See https://radiant-rstats.github.io/docs/data/pivotr.html for an example in Radiant

Examples

pivotr(diamonds, cvars = "cut") %>% str()
#> List of 15 #> $ cni : logi [1:2] FALSE FALSE #> $ cn : chr [1:2] "cut" "n_obs" #> $ tab_freq : tibble [6 × 2] (S3: tbl_df/tbl/data.frame) #> ..$ cut : Factor w/ 6 levels "Fair","Good",..: 1 2 3 4 5 6 #> ..$ n_obs: int [1:6] 101 275 677 771 1176 3000 #> $ tab :'data.frame': 6 obs. of 2 variables: #> ..$ cut : Factor w/ 6 levels "Fair","Good",..: 1 2 3 4 5 6 #> ..$ n_obs: int [1:6] 101 275 677 771 1176 3000 #> ..- attr(*, "radiant_nrow")= num 5 #> $ df_name : chr "diamonds" #> $ fill : int 0 #> $ vars : chr "cut" #> $ cvars : chr "cut" #> $ nvar : chr "n_obs" #> $ fun : chr "mean" #> $ normalize : chr "None" #> $ tabfilt : chr "" #> $ tabsort : chr "" #> $ nr : num Inf #> $ data_filter: chr "" #> - attr(*, "class")= chr [1:2] "pivotr" "list"
pivotr(diamonds, cvars = "cut")$tab
#> cut n_obs #> 1 Fair 101 #> 2 Good 275 #> 3 Very Good 677 #> 4 Premium 771 #> 5 Ideal 1176 #> 6 Total 3000
pivotr(diamonds, cvars = c("cut","clarity","color"))$tab
#> clarity color Fair Good Very_Good Premium Ideal Total #> 1 I1 D 0 1 0 3 0 4 #> 2 I1 E 1 1 2 1 0 5 #> 3 I1 F 2 1 2 2 4 11 #> 4 I1 G 1 1 1 2 0 5 #> 5 I1 H 3 0 1 3 1 8 #> 6 I1 I 4 0 0 1 0 5 #> 7 I1 J 1 0 0 1 0 2 #> 8 SI2 D 8 14 18 13 15 68 #> 9 SI2 E 5 16 30 25 30 106 #> 10 SI2 F 1 11 24 23 34 93 #> 11 SI2 G 5 7 29 35 21 97 #> 12 SI2 H 4 7 14 31 30 86 #> 13 SI2 I 2 2 7 21 14 46 #> 14 SI2 J 3 5 10 9 6 33 #> 15 SI1 D 4 9 21 38 39 111 #> 16 SI1 E 5 22 37 47 36 147 #> 17 SI1 F 4 14 40 25 42 125 #> 18 SI1 G 5 10 23 26 31 95 #> 19 SI1 H 9 13 21 31 43 117 #> 20 SI1 I 1 14 18 20 23 76 #> 21 SI1 J 1 11 11 9 18 50 #> 22 VS2 D 2 3 17 12 55 89 #> 23 VS2 E 2 15 26 41 55 139 #> 24 VS2 F 4 12 23 36 53 128 #> 25 VS2 G 2 8 17 42 50 119 #> 26 VS2 H 2 10 20 25 31 88 #> 27 VS2 I 2 2 16 17 19 56 #> 28 VS2 J 0 2 12 12 16 42 #> 29 VS1 D 0 3 10 13 24 50 #> 30 VS1 E 0 5 9 13 25 52 #> 31 VS1 F 4 8 25 17 29 83 #> 32 VS1 G 2 7 22 30 51 112 #> 33 VS1 H 3 3 19 23 22 70 #> 34 VS1 I 2 5 11 14 18 50 #> 35 VS1 J 2 2 3 10 8 25 #> 36 VVS2 D 0 4 8 9 20 41 #> 37 VVS2 E 1 1 25 7 22 56 #> 38 VVS2 F 0 0 14 6 29 49 #> 39 VVS2 G 1 3 21 10 43 78 #> 40 VVS2 H 0 3 12 5 13 33 #> 41 VVS2 I 0 1 1 4 14 20 #> 42 VVS2 J 0 0 2 2 3 7 #> 43 VVS1 D 1 1 2 4 9 17 #> 44 VVS1 E 0 2 7 7 21 37 #> 45 VVS1 F 1 7 4 4 29 45 #> 46 VVS1 G 0 3 16 9 42 70 #> 47 VVS1 H 0 1 6 11 14 32 #> 48 VVS1 I 0 1 4 3 12 20 #> 49 VVS1 J 0 0 1 1 1 3 #> 50 IF D 0 0 1 0 1 2 #> 51 IF E 0 0 4 3 5 12 #> 52 IF F 1 2 4 6 18 31 #> 53 IF G 0 1 2 2 16 21 #> 54 IF H 0 0 2 3 15 20 #> 55 IF I 0 1 2 3 5 11 #> 56 IF J 0 0 0 1 1 2 #> 57 Total Total 101 275 677 771 1176 3000
pivotr(diamonds, cvars = "cut:clarity", nvar = "price")$tab
#> clarity Fair Good Very_Good Premium Ideal Total #> 1 I1 2730.167 4333.500 3864.167 4932.231 6078.200 4194.775 #> 2 SI2 5893.964 5280.919 5045.621 5568.019 4435.673 5100.189 #> 3 SI1 4273.069 3757.022 4277.544 4113.811 3758.125 3998.577 #> 4 VS2 3292.000 3925.481 3950.947 4522.914 3306.290 3822.967 #> 5 VS1 5110.769 3740.697 3889.475 4461.333 3189.362 3789.181 #> 6 VVS2 2030.500 4378.167 2525.193 3580.581 3665.181 3337.820 #> 7 VVS1 6761.500 3889.333 1945.875 1426.692 2960.594 2608.460 #> 8 IF 3205.000 817.250 4675.867 2361.333 1961.344 2411.697 #> 9 Total 4505.238 4130.433 3959.916 4369.409 3470.224 3907.186
pivotr(diamonds, cvars = "cut", nvar = "price")$tab
#> cut price #> 1 Fair 4505.238 #> 2 Good 4130.433 #> 3 Very Good 3959.916 #> 4 Premium 4369.409 #> 5 Ideal 3470.224 #> 6 Total 3907.186
pivotr(diamonds, cvars = "cut", normalize = "total")$tab
#> cut n_obs #> 1 Fair 0.03366667 #> 2 Good 0.09166667 #> 3 Very Good 0.22566667 #> 4 Premium 0.25700000 #> 5 Ideal 0.39200000 #> 6 Total 1.00000000