pairbind: Append two datasets' rows in a pairwise fashion

Description Usage Arguments Details Value See Also Examples

View source: R/pairbind.R

Description

Append two datasets' rows in a pairwise fashion. In other words, append the 1st row of the first dataset with the 1st row of the second dataset, the 2nd row of the first with the 2nd row of the second, and so on.

Usage

1
pairbind(x, y), pairbind_df(x, y)

Arguments

x

2D object (e.g. Data frame, matrix)

y

Same as above.

Details

This function can be useful for kable-friendly frequency distribution tables.

Value

Same as above; except in the case of pairbind_df, which outputs a data frame.

See Also

https://github.com/robertschnitman/afp, lapply, do.call, rbind, https://purrr.tidyverse.org/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(tidyverse)
library(knitr)

freq_df <- with(diamonds, table(color, clarity)) %>%
  as.data.frame() %>%
  spread(NCOL(.) - 1, NCOL(.)) %>%
  mutate(Total = apply(.[, 2:NCOL(.)], 1, sum))

 prop_df <- freq_df
 prop_df[, 2:NCOL(prop_df)] <- prop_df[, 2:NCOL(prop_df)]/prop_df$Total
 prop_df[, 2:NCOL(prop_df)] %<>% 
   map_df(~ paste0(round(.x*100), '%'))
  freq_df %<>% 
  format(., big.mark = ',', scientific = FALSE) %>%
  map_df(as.character)
  
prop_df %<>% map_df(as.character)

pairbind_df(freq_df, prop_df) %>% kable()

robertschnitman/afp documentation built on Aug. 9, 2020, 9:46 a.m.