Browse Source

Using purrr for list/functions manipulations

tags/0.1.0
Maxime Wack 8 years ago
parent
commit
79c99b6eeb
3 changed files with 12 additions and 8 deletions
  1. +2
    -1
      DESCRIPTION
  2. +3
    -0
      NAMESPACE
  3. +7
    -7
      R/build.R

+ 2
- 1
DESCRIPTION View File

@@ -13,7 +13,8 @@ License: GPL-3
Encoding: UTF-8
LazyData: true
Imports:
dplyr
dplyr,
purrr
Suggests:
datatable,
pander


+ 3
- 0
NAMESPACE View File

@@ -2,3 +2,6 @@

export("%>%")
importFrom(dplyr,"%>%")
importFrom(purrr,flatten_dbl)
importFrom(purrr,map)
importFrom(purrr,map_lgl)

+ 7
- 7
R/build.R View File

@@ -3,11 +3,12 @@
#' @param stat The statistic to use
#' @param data The dataframe to apply the statistic to
#' @return A vector for one statistic column
#' @importFrom purrr map flatten_dbl
statColumn <- function(stat, data)
{
data %>%
lapply(stat) %>%
unlist
map(stat) %>%
flatten_dbl
}

#' Generate the table of all statistics for all variables
@@ -38,6 +39,7 @@ statTable <- function(data, stats)
#' @param data The dataframe to get the names from
#' @param labels The optional named character vector containing the keypairs var = "Label"
#' @return A character vector of variable names/labels/levels
#' @importFrom purrr map map_lgl
varColumn <- function(data, labels = NULL)
{
# Replace variable names by their labels, if they exist
@@ -45,18 +47,16 @@ varColumn <- function(data, labels = NULL)
base_names[base_names %in% names(labels)] <- labels[base_names[base_names %in% names(labels)]]

# Insert levels for factors after the variable name
if (any(data %>% lapply(is.factor) %>% unlist))
if (any(data %>% map_lgl(is.factor)))
{
data %>%
lapply(is.factor) %>%
unlist %>%
map_lgl(is.factor) %>%
which %>%
insert(x = base_names,
y = select(data, .) %>% lapply(levels),
y = select(data, .) %>% map(levels),
position = .)
} else
{
base_names
}
}


Loading…
Cancel
Save