├── .Rbuildignore ├── .gitignore ├── .travis.yml ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R ├── append_df.R ├── col_name.R ├── fuse.R ├── mutate_which.R ├── pull.R ├── reexports_bazar.R ├── reexports_dplyr.R ├── reexports_magrittr.R ├── rows_.R ├── take.R └── verbs_for_lists.R ├── README.md ├── lplyr.Rproj ├── man ├── fuse.Rd ├── mutate_.list.Rd ├── mutate_which.Rd ├── pull.Rd ├── reexports.Rd └── take.Rd ├── tests ├── testthat.R └── testthat │ └── test-test.R └── vignettes ├── .gitignore ├── lplyr-vignette.R ├── lplyr-vignette.Rmd └── lplyr-vignette.html /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .travis.yml 4 | ^cran-comments\.md$ 5 | TODO -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | inst/doc 5 | TODO 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r 2 | 3 | language: R 4 | cache: packages 5 | warnings_are_errors: false 6 | r_github_packages: 7 | - hadley/multidplyr -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: lplyr 2 | Type: Package 3 | Title: 'dplyr' Verbs for Lists and Other Verbs for Data Frames 4 | Version: 0.1.12 5 | Date: 2017-11-03 6 | Authors@R: person("Paul", "Poncet", , "paulponcet@yahoo.fr", role = c("aut", "cre")) 7 | Description: Provides 'dplyr' verbs for lists and other useful 8 | verbs for manipulation of data frames. In particular, it includes a 9 | mutate_which() function that mutates columns for a specific subset of 10 | rows defined by a condition, and fuse() which is a more flexible version 11 | of 'tidyr' unite() function. 12 | License: MIT + file LICENSE 13 | LazyData: TRUE 14 | Depends: 15 | R (>= 3.1.3) 16 | Imports: 17 | bazar, 18 | dplyr, 19 | lazyeval, 20 | magrittr 21 | Suggests: 22 | knitr, 23 | rmarkdown, 24 | testthat, 25 | tidyr 26 | VignetteBuilder: 27 | knitr 28 | URL: https://github.com/paulponcet/lplyr 29 | BugReports: https://github.com/paulponcet/lplyr/issues 30 | RoxygenNote: 6.0.1 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Paul Poncet -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(fuse_,data.frame) 4 | S3method(fuse_,tbl_df) 5 | S3method(mutate_,list) 6 | S3method(mutate_,pairlist) 7 | S3method(pull_,data.frame) 8 | S3method(pull_,list) 9 | S3method(pull_,matrix) 10 | S3method(rename_,list) 11 | S3method(rename_,pairlist) 12 | S3method(take_,data.frame) 13 | S3method(take_,tbl_df) 14 | export("%>%") 15 | export(concat) 16 | export(concat0) 17 | export(concat_) 18 | export(fuse) 19 | export(fuse_) 20 | export(mutate) 21 | export(mutate_) 22 | export(mutate_which) 23 | export(mutate_which_) 24 | export(pull) 25 | export(pull_) 26 | export(rename) 27 | export(rename_) 28 | export(select) 29 | export(select_) 30 | export(take) 31 | export(take_) 32 | export(transmute) 33 | export(transmute_) 34 | export(transmute_which) 35 | export(transmute_which_) 36 | importFrom(bazar,concat) 37 | importFrom(bazar,concat0) 38 | importFrom(bazar,concat_) 39 | importFrom(bazar,is.wholenumber) 40 | importFrom(dplyr,filter_) 41 | importFrom(dplyr,mutate) 42 | importFrom(dplyr,mutate_) 43 | importFrom(dplyr,rename) 44 | importFrom(dplyr,rename_) 45 | importFrom(dplyr,rename_vars_) 46 | importFrom(dplyr,select) 47 | importFrom(dplyr,select_) 48 | importFrom(dplyr,select_vars) 49 | importFrom(dplyr,tbl_df) 50 | importFrom(dplyr,transmute) 51 | importFrom(dplyr,transmute_) 52 | importFrom(lazyeval,all_dots) 53 | importFrom(lazyeval,f_rhs) 54 | importFrom(lazyeval,is_formula) 55 | importFrom(lazyeval,lazy) 56 | importFrom(lazyeval,lazy_dots) 57 | importFrom(lazyeval,lazy_eval) 58 | importFrom(magrittr,"%>%") 59 | importFrom(stats,setNames) 60 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # lplyr 0.1.12 2 | 3 | * 'chunk()' is removed since 'multidplyr' package is no longer on CRAN 4 | for R 3.4.X. 5 | 6 | 7 | # lplyr 0.1.11 8 | 9 | * 'select()' functions are removed due to conflicts with the existing 10 | 'select.list()' function in package 'utils'. 11 | 12 | 13 | # lplyr 0.1.10 14 | 15 | * drop_narows() function added (not exported for now) 16 | 17 | 18 | # lplyr 0.1.9 19 | 20 | * take() function added 21 | 22 | 23 | # lplyr 0.1.8 24 | 25 | * take() function added 26 | 27 | 28 | # lplyr 0.1.7 29 | 30 | * Issue #2: chunck() now renamed chunk() 31 | * mutate_if_which() removed for now (not ready yet, 32 | and adequate name to be found) 33 | 34 | 35 | # lplyr 0.1.6 36 | 37 | * subset() function removed for now (not ready yet) 38 | 39 | 40 | # lplyr 0.1.5 41 | 42 | * subset() function added 43 | 44 | 45 | # lplyr 0.1.4 46 | 47 | * Test for miniCRAN 48 | * Changed R file names to clarify what is reexported. 49 | * Removed chunck() function provisionally. 50 | 51 | 52 | # lplyr 0.1.3 53 | 54 | * Changed 'base2' for 'bazar' 55 | 56 | 57 | # lplyr 0.1.2 58 | 59 | * fuse() function added 60 | 61 | 62 | # lplyr 0.1.1 63 | 64 | * 'dplyr' verbs added for pairlists 65 | * Updated README.md file 66 | * Vignette created 67 | 68 | 69 | # lplyr 0.1.0 70 | 71 | * Creation of the package 'lplyr' 72 | -------------------------------------------------------------------------------- /R/append_df.R: -------------------------------------------------------------------------------- 1 | 2 | # from tidyr 3 | append_df <- 4 | function(x, 5 | values, 6 | after = length(x)) 7 | { 8 | y <- append(x, values, after = after) 9 | class(y) <- class(x) 10 | attr(y, "row.names") <- attr(x, "row.names") 11 | y 12 | } 13 | -------------------------------------------------------------------------------- /R/col_name.R: -------------------------------------------------------------------------------- 1 | 2 | # from tidyr 3 | col_name <- 4 | function (x, 5 | default = stop("Please supply column name", call. = FALSE)) 6 | { 7 | if (is.character(x)) 8 | return(x) 9 | if (identical(x, quote(expr = ))) 10 | return(default) 11 | if (is.name(x)) 12 | return(as.character(x)) 13 | if (is.null(x)) 14 | return(x) 15 | stop("Invalid column specification", call. = FALSE) 16 | } 17 | -------------------------------------------------------------------------------- /R/fuse.R: -------------------------------------------------------------------------------- 1 | #' @title 2 | #' Fuse multiple columns into one 3 | #' 4 | #' @description 5 | #' \code{fuse} is a more flexible version of \code{\link[tidyr]{unite}} 6 | #' from package \pkg{tidyr}. 7 | #' 8 | #' @note 9 | #' This function has been inspired by the issue raised at 10 | #' \url{https://github.com/tidyverse/tidyr/issues/203}. 11 | #' 12 | #' @param .data 13 | #' A tbl or data.frame 14 | #' 15 | #' @param col 16 | #' character. (Bare) name of column to add 17 | #' 18 | #' @param ... 19 | #' Specification of columns to fuse. 20 | #' 21 | #' @param from 22 | #' character. A vector of the names of columns to fuse. 23 | #' 24 | #' @param fun 25 | #' function. The function to be applied (\code{concat0} by default). 26 | #' 27 | #' @param remove 28 | #' logical. If \code{TRUE} (the default), 29 | #' remove input columns from output data frame. 30 | #' 31 | #' @seealso 32 | #' \code{\link[tidyr]{unite}} and 33 | #' \code{\link[tidyr]{unite_}} from package \pkg{tidyr}; 34 | #' 35 | #' \code{\link[bazar]{concat0}} from package \pkg{bazar}. 36 | #' 37 | #' @importFrom dplyr select_vars 38 | #' @export 39 | #' 40 | #' @examples 41 | #' df <- data.frame(x = c(NA, "a", NA), 42 | #' y = c("b", NA, NA)) 43 | #' fuse(df, "z", x, y) 44 | #' 45 | #' # To be compared with: 46 | #' tidyr::unite(df, "z", x, y, sep = "") 47 | #' # The same 48 | #' fuse(df, "z", x, y, fun = function(x) concat0(x, na.rm = FALSE)) 49 | #' 50 | fuse <- 51 | function(.data, 52 | col, 53 | ..., 54 | fun = concat0, 55 | remove = TRUE) 56 | { 57 | col <- col_name(substitute(col)) 58 | from <- dplyr::select_vars(colnames(.data), ...) 59 | fuse_(.data, col, from, fun = fun, remove = remove) 60 | } 61 | 62 | 63 | #' @export 64 | #' @rdname fuse 65 | #' 66 | fuse_ <- 67 | function(.data, 68 | col, 69 | from, 70 | fun = concat0, 71 | #..., 72 | remove = TRUE) 73 | { 74 | UseMethod("fuse_") 75 | } 76 | 77 | 78 | #' @importFrom dplyr tbl_df 79 | #' @export 80 | #' @rdname fuse 81 | #' 82 | fuse_.data.frame <- 83 | function(.data, 84 | col, 85 | from, 86 | fun = concat0, 87 | #..., 88 | remove = TRUE) 89 | { 90 | .data <- dplyr::tbl_df(.data) 91 | as.data.frame(fuse_(.data, col, from, fun = fun, #..., 92 | remove = remove)) 93 | } 94 | 95 | 96 | #' @importFrom stats setNames 97 | #' @export 98 | #' @rdname fuse 99 | #' 100 | fuse_.tbl_df <- 101 | function(.data, 102 | col, 103 | from, 104 | fun = concat0, 105 | #..., 106 | remove = TRUE) 107 | { 108 | #united <- do.call(fun, .data[from])#c(.data[from], list(...))) 109 | united <- apply(.data[from], 1, FUN = fun) 110 | first_col <- which(names(.data) %in% from)[1L] 111 | if (remove) { 112 | .data <- .data[setdiff(names(.data), from)] 113 | } 114 | name <- enc2utf8(col) 115 | append_df(.data, stats::setNames(list(united), name), 116 | after = first_col - 1L) 117 | } 118 | -------------------------------------------------------------------------------- /R/mutate_which.R: -------------------------------------------------------------------------------- 1 | #' @title 2 | #' Add new variables or modify existing ones 3 | #' on a subset of the data 4 | #' 5 | #' @description 6 | #' The functions \code{mutate_which} and \code{transmute_which} are 7 | #' similar to \code{\link[dplyr]{mutate}} and \code{\link[dplyr]{transmute}} 8 | #' from package \pkg{dplyr}, except that they work only on a subset 9 | #' of \code{.data}, this subset being defined by the \code{.condition}. 10 | #' 11 | #' The functions \code{mutate_which_} and \code{transmute_which_} are 12 | #' standard evaluation versions, similar to \code{\link[dplyr]{mutate_}} and 13 | #' \code{\link[dplyr]{transmute_}}. 14 | #' 15 | #' @param .data 16 | #' A tbl or data.frame. 17 | #' 18 | #' @param .condition 19 | #' A condition defining the subset on which the mutate 20 | #' or transmute operation applies. 21 | #' New variables are initialized to \code{NA}. 22 | #' 23 | #' @param ... 24 | #' Name-value pairs of expressions. Use \code{NULL} to drop a variable. 25 | #' 26 | #' @param .dots 27 | #' Used to work around non-standard evaluation. 28 | #' 29 | #' @return 30 | #' A tbl or a data frame, depending on the class of \code{.data}. 31 | #' 32 | #' @author 33 | #' Adapted from G. Grothendieck on StackOverflow, see 34 | #' \url{http://stackoverflow.com/a/34096575}. 35 | #' 36 | #' @seealso \code{\link[dplyr]{mutate}}, 37 | #' \code{\link[dplyr]{mutate_}}, 38 | #' \code{\link[dplyr]{transmute}}, 39 | #' \code{\link[dplyr]{transmute_}} from package \pkg{dplyr}. 40 | #' 41 | #' @importFrom dplyr mutate_ 42 | #' @importFrom lazyeval lazy_dots 43 | #' @export 44 | #' 45 | #' @examples 46 | #' df <- mtcars[1:10,] 47 | #' 48 | #' # Non-standard evaluation 49 | #' mutate_which(df, gear==4, carb = 100) 50 | #' transmute_which(df, gear==4, carb = 100) 51 | #' 52 | #' # Standard evaluation 53 | #' mutate_which_(df, ~ gear==4, carb = ~ 100) 54 | #' transmute_which_(df, ~ gear==4, carb = ~ 100) 55 | #' 56 | mutate_which <- 57 | function(.data, 58 | .condition, 59 | ...) 60 | { 61 | dots <- lazyeval::lazy_dots(...) 62 | .condition <- lazyeval::lazy(.condition) 63 | mutate_which_(.data, .condition, .dots = dots) 64 | } 65 | 66 | 67 | #' @importFrom dplyr mutate_ 68 | #' @importFrom lazyeval lazy_dots 69 | #' @export 70 | #' @rdname mutate_which 71 | #' 72 | mutate_which_ <- 73 | function(.data, 74 | .condition, 75 | ..., 76 | .dots) 77 | { 78 | dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) 79 | expr <- lazyeval::all_dots(.condition)[[1]]$expr 80 | .condition <- eval(expr, .data, parent.frame())#, parent.frame())#, envir = list(.data, parent.frame())) 81 | 82 | # New variables are initialized to NA 83 | n <- setdiff(names(dots), names(.data)) 84 | .data[, n] <- NA 85 | 86 | w <- rows_(.condition, .data) 87 | .data[w, ] <- dplyr::mutate_(.data[w, ], .dots = dots) 88 | .data 89 | } 90 | 91 | 92 | #' @importFrom dplyr select_ 93 | #' @importFrom lazyeval lazy_dots 94 | #' @export 95 | #' @rdname mutate_which 96 | #' 97 | transmute_which <- 98 | function (.data, 99 | .condition, 100 | ...) 101 | { 102 | dots <- lazyeval::lazy_dots(...) 103 | .condition <- lazyeval::lazy(.condition) 104 | transmute_which_(.data, .condition, .dots = dots) 105 | } 106 | 107 | 108 | #' @importFrom dplyr select_ 109 | #' @importFrom lazyeval all_dots 110 | #' @export 111 | #' @rdname mutate_which 112 | #' 113 | transmute_which_ <- 114 | function (.data, 115 | .condition, 116 | ..., 117 | .dots) 118 | { 119 | dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) 120 | out <- mutate_which_(.data, .condition, .dots = dots) 121 | keep <- names(dots) 122 | dplyr::select_(out, keep) 123 | } 124 | -------------------------------------------------------------------------------- /R/pull.R: -------------------------------------------------------------------------------- 1 | #' @title 2 | #' Column selection 3 | #' 4 | #' @description 5 | #' The function \code{pull} selects a column in a data frame 6 | #' and transforms it into a vector. 7 | #' This is useful to use it in combination with \pkg{magrittr}'s pipe operator 8 | #' and \pkg{dplyr}'s verbs. 9 | #' 10 | #' @param .data 11 | #' A tbl. 12 | #' 13 | #' @param j 14 | #' integer. The column to be extracted. 15 | #' 16 | #' @return 17 | #' A vector of length \code{nrow(.data)} 18 | #' 19 | #' @author Adapted from Tommy O' Dell, 20 | #' see \url{http://stackoverflow.com/a/24730843/3902976} on StackOverflow. 21 | #' 22 | #' @export 23 | #' 24 | #' @examples 25 | #' library(dplyr) 26 | #' mtcars[["mpg"]] 27 | #' mtcars %>% pull(mpg) 28 | #' 29 | #' # more convenient than (mtcars %>% filter(mpg > 20))[[3L]] 30 | #' mtcars %>% 31 | #' filter(mpg > 20) %>% 32 | #' pull(3) 33 | #' 34 | pull <- 35 | function(.data, 36 | j) 37 | { 38 | j <- if (is.name(substitute(j))) deparse(substitute(j)) else j 39 | pull_(.data, j) 40 | } 41 | 42 | 43 | #' @importFrom lazyeval is_formula 44 | #' @importFrom lazyeval f_rhs 45 | #' @export 46 | #' @rdname pull 47 | #' 48 | pull_ <- 49 | function(.data, 50 | j) 51 | { 52 | UseMethod("pull_") 53 | } 54 | 55 | 56 | #' @export 57 | #' @rdname pull 58 | #' 59 | pull_.data.frame <- 60 | function(.data, 61 | j) 62 | { 63 | j <- if (lazyeval::is_formula(j)) as.character(lazyeval::f_rhs(j)) else j 64 | .data[, j, drop = FALSE][[1L]] 65 | } 66 | 67 | 68 | #' @export 69 | #' @rdname pull 70 | #' 71 | pull_.matrix <- 72 | function(.data, 73 | j) 74 | { 75 | pull_(as.data.frame(.data)) 76 | } 77 | 78 | 79 | #' @export 80 | #' @rdname pull 81 | #' 82 | pull_.list <- 83 | function(.data, 84 | j) 85 | { 86 | j <- if (lazyeval::is_formula(j)) as.character(lazyeval::f_rhs(j)) else j 87 | .data[[j]] 88 | } 89 | -------------------------------------------------------------------------------- /R/reexports_bazar.R: -------------------------------------------------------------------------------- 1 | 2 | #' @importFrom bazar concat 3 | #' @export 4 | #' 5 | bazar::concat 6 | 7 | 8 | #' @importFrom bazar concat0 9 | #' @export 10 | #' 11 | bazar::concat0 12 | 13 | 14 | #' @importFrom bazar concat_ 15 | #' @export 16 | #' 17 | bazar::concat_ 18 | 19 | -------------------------------------------------------------------------------- /R/reexports_dplyr.R: -------------------------------------------------------------------------------- 1 | 2 | #' @importFrom dplyr mutate 3 | #' @export 4 | #' 5 | dplyr::mutate 6 | 7 | 8 | #' @importFrom dplyr mutate_ 9 | #' @export 10 | #' 11 | dplyr::mutate_ 12 | 13 | 14 | #' @importFrom dplyr pull 15 | #' @export 16 | #' 17 | dplyr::pull 18 | 19 | 20 | #' @importFrom dplyr rename 21 | #' @export 22 | #' 23 | dplyr::rename 24 | 25 | 26 | #' @importFrom dplyr rename_ 27 | #' @export 28 | #' 29 | dplyr::rename_ 30 | 31 | 32 | #' @importFrom dplyr select 33 | #' @export 34 | #' 35 | dplyr::select 36 | 37 | 38 | #' @importFrom dplyr select_ 39 | #' @export 40 | #' 41 | dplyr::select_ 42 | 43 | 44 | #' @importFrom dplyr transmute 45 | #' @export 46 | #' 47 | dplyr::transmute 48 | 49 | 50 | #' @importFrom dplyr transmute_ 51 | #' @export 52 | #' 53 | dplyr::transmute_ 54 | -------------------------------------------------------------------------------- /R/reexports_magrittr.R: -------------------------------------------------------------------------------- 1 | 2 | #' @importFrom magrittr %>% 3 | #' @export 4 | #' 5 | magrittr::`%>%` 6 | -------------------------------------------------------------------------------- /R/rows_.R: -------------------------------------------------------------------------------- 1 | 2 | #' @importFrom bazar is.wholenumber 3 | #' 4 | rows_ <- 5 | function(.condition, 6 | .data) 7 | { 8 | n <- nrow(.data) 9 | if (missing(.condition) || is.null(.condition)) { 10 | w <- seq_len(n) 11 | } else if (is.logical(.condition) && length(.condition)==n) { 12 | w <- which(.condition) 13 | } else if (is.logical(.condition) && length(.condition)==1L) { 14 | w <- which(rep(.condition, n)) 15 | } else if (bazar::is.wholenumber(.condition)) { 16 | w <- .condition 17 | } else { 18 | stop("incorrect '.condition' argument") 19 | } 20 | w 21 | } 22 | -------------------------------------------------------------------------------- /R/take.R: -------------------------------------------------------------------------------- 1 | #' @title 2 | #' Subset data frames 3 | #' 4 | #' @description 5 | #' Return subset of a data frame which meets conditions. 6 | #' 7 | #' @param .data 8 | #' A tbl or data.frame. 9 | #' 10 | #' @param .condition 11 | #' A condition defining the \code{\link[dplyr]{filter}} to be applied on 12 | #' \code{.data}. 13 | #' 14 | #' @param ... 15 | #' Variable names to be \code{\link[dplyr]{select}}ed. 16 | #' 17 | #' @param .dots 18 | #' character vector of variable names to be \code{\link[dplyr]{select}}ed. 19 | #' 20 | #' @return 21 | #' A tbl or data.frame 22 | #' 23 | #' @seealso \code{\link[dplyr]{filter}} and \code{\link[dplyr]{select}} 24 | #' from package \pkg{dplyr}. 25 | #' 26 | #' @importFrom lazyeval lazy 27 | #' @importFrom lazyeval lazy_dots 28 | #' @export 29 | #' 30 | #' @examples 31 | #' df <- mtcars[1:10,] 32 | #' take(df, cyl %in% c(4, 6), mpg, disp) 33 | #' take_(df, ~ cyl %in% c(4, 6), ~ mpg, ~ disp) 34 | #' take_(df, ~ cyl %in% c(4, 6), .dots = c("mpg", "disp")) 35 | #' 36 | take <- 37 | function(.data, 38 | .condition, 39 | ...) 40 | { 41 | dots <- lazyeval::lazy_dots(...) 42 | .condition <- lazyeval::lazy(.condition) 43 | take_(.data, .condition, .dots = dots) 44 | } 45 | 46 | 47 | #' @export 48 | #' @rdname take 49 | #' 50 | take_ <- 51 | function(.data, 52 | .condition, 53 | ..., 54 | .dots) 55 | { 56 | UseMethod("take_") 57 | } 58 | 59 | 60 | #' @importFrom dplyr tbl_df 61 | #' @importFrom lazyeval all_dots 62 | #' @export 63 | #' @rdname take 64 | #' 65 | take_.data.frame <- 66 | function(.data, 67 | .condition, 68 | ..., 69 | .dots) 70 | { 71 | dots <- lazyeval::all_dots(.dots, ...) 72 | as.data.frame(take_(dplyr::tbl_df(.data), .condition = .condition, .dots = dots)) 73 | } 74 | 75 | 76 | #' @importFrom dplyr filter_ 77 | #' @importFrom dplyr select_ 78 | #' @importFrom lazyeval all_dots 79 | #' @export 80 | #' @rdname take 81 | #' 82 | take_.tbl_df <- 83 | function(.data, 84 | .condition, 85 | ..., 86 | .dots) 87 | { 88 | dots <- lazyeval::all_dots(.dots, ...) 89 | .data <- dplyr::filter_(.data, .dots = .condition) 90 | dplyr::select_(.data, .dots = dots) 91 | } 92 | -------------------------------------------------------------------------------- /R/verbs_for_lists.R: -------------------------------------------------------------------------------- 1 | #' @title 2 | #' Dplyr verbs for lists and pairlists 3 | #' 4 | #' @description 5 | #' We add methods for the verbs \code{\link[dplyr]{mutate}}, 6 | #' \code{\link[dplyr]{rename}}. 7 | #' 8 | #' @param .data 9 | #' A list or pairlist. 10 | #' 11 | #' @param ... 12 | #' Comma separated list of unquoted expressions. 13 | #' 14 | #' @param .dots 15 | #' Used to work around non-standard evaluation. 16 | #' 17 | #' @return 18 | #' A list or a pairlist. 19 | #' 20 | #' @seealso \code{\link[dplyr]{mutate}}, 21 | #' \code{\link[dplyr]{rename}} from package \pkg{dplyr}. 22 | #' 23 | #' @importFrom lazyeval all_dots 24 | #' @importFrom lazyeval lazy_eval 25 | #' @export 26 | #' 27 | #' @examples 28 | #' xs <- list(x1 = 1:3, 29 | #' x2 = 2:5, 30 | #' x3 = list("alpha", c("beta", "gamma"))) 31 | #' 32 | #' # Non-standard evaluation 33 | #' mutate(xs, x4 = 4) 34 | #' rename(xs, x0 = x1) 35 | #' 36 | #' # Standard evaluation 37 | #' mutate_(xs, x4 = ~ 4) 38 | #' rename_(xs, x0 = ~ x1) 39 | #' 40 | mutate_.list <- 41 | function(.data, ..., .dots) 42 | { 43 | dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) 44 | c(.data, lazyeval::lazy_eval(dots)) 45 | } 46 | 47 | 48 | #' @export 49 | #' @rdname mutate_.list 50 | #' 51 | mutate_.pairlist <- mutate_.list 52 | 53 | 54 | #' @importFrom dplyr rename_vars_ 55 | #' @importFrom lazyeval all_dots 56 | #' @export 57 | #' @rdname mutate_.list 58 | #' 59 | rename_.list <- 60 | function(.data, ..., .dots) 61 | { 62 | dots <- lazyeval::all_dots(.dots, ...) 63 | vars <- dplyr::rename_vars_(names(.data), dots) 64 | names(.data) <- names(vars) 65 | .data 66 | } 67 | 68 | 69 | #' @export 70 | #' @rdname mutate_.list 71 | #' 72 | rename_.pairlist <- rename_.list 73 | 74 | 75 | # #' @importFrom dplyr select_vars_ 76 | # #' @importFrom lazyeval all_dots 77 | # #' @export 78 | # #' @rdname mutate_.list 79 | # #' 80 | # select_.list <- 81 | # function(.data, ..., .dots) 82 | # { 83 | # dots <- lazyeval::all_dots(.dots, ...) 84 | # vars <- dplyr::select_vars_(names(.data), dots) 85 | # .data[vars] 86 | # } 87 | 88 | 89 | # #' @export 90 | # #' @rdname mutate_.list 91 | # #' 92 | # select_.pairlist <- select_.list 93 | 94 | 95 | # todo 96 | #summarise_.list <- 97 | #function(.data, ..., .dots) 98 | #{ 99 | # dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) 100 | # 101 | #} 102 | 103 | 104 | # #' @importFrom dplyr mutate_ 105 | # #' @importFrom dplyr select_ 106 | # #' @importFrom lazyeval all_dots 107 | # #' @export 108 | # #' @rdname mutate_.list 109 | # #' 110 | # transmute_.list <- 111 | # function(.data, ..., .dots) 112 | # { 113 | # dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) 114 | # out <- dplyr::mutate_(.data, .dots = dots) 115 | # keep <- names(dots) 116 | # dplyr::select_(out, .dots = keep) 117 | # } 118 | 119 | 120 | # #' @export 121 | # #' @rdname mutate_.list 122 | # #' 123 | # transmute_.pairlist <- transmute_.list 124 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lplyr: dplyr verbs for lists and other verbs for data frames 2 | 3 | [![Travis-CI Build Status](https://travis-ci.org/paulponcet/lplyr.svg?branch=master)](https://travis-ci.org/paulponcet/lplyr) [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/lplyr)](https://cran.r-project.org/package=lplyr) [![](https://cranlogs.r-pkg.org/badges/lplyr)](https://cran.r-project.org/package=lplyr) 4 | 5 | 6 | ## Installation 7 | 8 | You can install 'lplyr' from GitHub with: 9 | 10 | ```R 11 | # install.packages("devtools") 12 | devtools::install_github("paulponcet/lplyr") 13 | ``` 14 | 15 | ## Verbs for lists and pairlists 16 | 17 | The package 'lplyr' extends some dplyr verbs to lists and pairlists: 18 | 19 | ```R 20 | library(lplyr) 21 | 22 | xs <- list(x1 = 1:3, 23 | x2 = 2:5, 24 | x3 = list("alpha", c("beta", "gamma"))) 25 | 26 | mutate(xs, x4 = 4) 27 | rename(xs, x0 = x1) 28 | ``` 29 | Usual verbs made for standard evaluation work as well: 30 | 31 | ```R 32 | mutate_(xs, x4 = ~ 4) 33 | rename_(xs, x0 = ~ x1) 34 | ``` 35 | 36 | 37 | ## New verbs for data frames 38 | 39 | The `mutate_which` and `transmute_which` functions are made for adding new variables or modifying existing ones on a subset of the data. 40 | 41 | ```R 42 | df <- mtcars[1:10,] 43 | mutate_which(df, gear==4, carb = 100) 44 | transmute_which(df, gear==4, carb = 100) 45 | ``` 46 | 47 | There is also a standard evaluation version of these functions, 48 | called `mutate_which_` and `transmute_which_`: 49 | 50 | ```R 51 | mutate_which_(df, ~ gear==4, carb = ~ 100) 52 | transmute_which_(df, ~ gear==4, carb = ~ 100) 53 | ``` 54 | 55 | The function `pull` selects a column in a data frame 56 | and transforms it into a vector. 57 | This is useful to use it in combination with 58 | magrittr's pipe operator and dplyr's verbs. 59 | 60 | ```R 61 | mtcars[["mpg"]] 62 | mtcars %>% pull(mpg) 63 | 64 | # more convenient than (mtcars %>% filter(mpg > 20))[[3L]] 65 | mtcars %>% 66 | filter(mpg > 20) %>% 67 | pull(3) 68 | ``` 69 | -------------------------------------------------------------------------------- /lplyr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageInstallArgs: --no-multiarch --with-keep.source 17 | PackageRoxygenize: rd,collate,namespace,vignette 18 | -------------------------------------------------------------------------------- /man/fuse.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fuse.R 3 | \name{fuse} 4 | \alias{fuse} 5 | \alias{fuse_} 6 | \alias{fuse_.data.frame} 7 | \alias{fuse_.tbl_df} 8 | \title{Fuse multiple columns into one} 9 | \usage{ 10 | fuse(.data, col, ..., fun = concat0, remove = TRUE) 11 | 12 | fuse_(.data, col, from, fun = concat0, remove = TRUE) 13 | 14 | \method{fuse_}{data.frame}(.data, col, from, fun = concat0, remove = TRUE) 15 | 16 | \method{fuse_}{tbl_df}(.data, col, from, fun = concat0, remove = TRUE) 17 | } 18 | \arguments{ 19 | \item{.data}{A tbl or data.frame} 20 | 21 | \item{col}{character. (Bare) name of column to add} 22 | 23 | \item{...}{Specification of columns to fuse.} 24 | 25 | \item{fun}{function. The function to be applied (\code{concat0} by default).} 26 | 27 | \item{remove}{logical. If \code{TRUE} (the default), 28 | remove input columns from output data frame.} 29 | 30 | \item{from}{character. A vector of the names of columns to fuse.} 31 | } 32 | \description{ 33 | \code{fuse} is a more flexible version of \code{\link[tidyr]{unite}} 34 | from package \pkg{tidyr}. 35 | } 36 | \note{ 37 | This function has been inspired by the issue raised at 38 | \url{https://github.com/tidyverse/tidyr/issues/203}. 39 | } 40 | \examples{ 41 | df <- data.frame(x = c(NA, "a", NA), 42 | y = c("b", NA, NA)) 43 | fuse(df, "z", x, y) 44 | 45 | # To be compared with: 46 | tidyr::unite(df, "z", x, y, sep = "") 47 | # The same 48 | fuse(df, "z", x, y, fun = function(x) concat0(x, na.rm = FALSE)) 49 | 50 | } 51 | \seealso{ 52 | \code{\link[tidyr]{unite}} and 53 | \code{\link[tidyr]{unite_}} from package \pkg{tidyr}; 54 | 55 | \code{\link[bazar]{concat0}} from package \pkg{bazar}. 56 | } 57 | -------------------------------------------------------------------------------- /man/mutate_.list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/verbs_for_lists.R 3 | \name{mutate_.list} 4 | \alias{mutate_.list} 5 | \alias{mutate_.pairlist} 6 | \alias{rename_.list} 7 | \alias{rename_.pairlist} 8 | \title{Dplyr verbs for lists and pairlists} 9 | \usage{ 10 | \method{mutate_}{list}(.data, ..., .dots) 11 | 12 | \method{mutate_}{pairlist}(.data, ..., .dots) 13 | 14 | \method{rename_}{list}(.data, ..., .dots) 15 | 16 | \method{rename_}{pairlist}(.data, ..., .dots) 17 | } 18 | \arguments{ 19 | \item{.data}{A list or pairlist.} 20 | 21 | \item{...}{Comma separated list of unquoted expressions.} 22 | 23 | \item{.dots}{Used to work around non-standard evaluation.} 24 | } 25 | \value{ 26 | A list or a pairlist. 27 | } 28 | \description{ 29 | We add methods for the verbs \code{\link[dplyr]{mutate}}, 30 | \code{\link[dplyr]{rename}}. 31 | } 32 | \examples{ 33 | xs <- list(x1 = 1:3, 34 | x2 = 2:5, 35 | x3 = list("alpha", c("beta", "gamma"))) 36 | 37 | # Non-standard evaluation 38 | mutate(xs, x4 = 4) 39 | rename(xs, x0 = x1) 40 | 41 | # Standard evaluation 42 | mutate_(xs, x4 = ~ 4) 43 | rename_(xs, x0 = ~ x1) 44 | 45 | } 46 | \seealso{ 47 | \code{\link[dplyr]{mutate}}, 48 | \code{\link[dplyr]{rename}} from package \pkg{dplyr}. 49 | } 50 | -------------------------------------------------------------------------------- /man/mutate_which.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mutate_which.R 3 | \name{mutate_which} 4 | \alias{mutate_which} 5 | \alias{mutate_which_} 6 | \alias{transmute_which} 7 | \alias{transmute_which_} 8 | \title{Add new variables or modify existing ones 9 | on a subset of the data} 10 | \usage{ 11 | mutate_which(.data, .condition, ...) 12 | 13 | mutate_which_(.data, .condition, ..., .dots) 14 | 15 | transmute_which(.data, .condition, ...) 16 | 17 | transmute_which_(.data, .condition, ..., .dots) 18 | } 19 | \arguments{ 20 | \item{.data}{A tbl or data.frame.} 21 | 22 | \item{.condition}{A condition defining the subset on which the mutate 23 | or transmute operation applies. 24 | New variables are initialized to \code{NA}.} 25 | 26 | \item{...}{Name-value pairs of expressions. Use \code{NULL} to drop a variable.} 27 | 28 | \item{.dots}{Used to work around non-standard evaluation.} 29 | } 30 | \value{ 31 | A tbl or a data frame, depending on the class of \code{.data}. 32 | } 33 | \description{ 34 | The functions \code{mutate_which} and \code{transmute_which} are 35 | similar to \code{\link[dplyr]{mutate}} and \code{\link[dplyr]{transmute}} 36 | from package \pkg{dplyr}, except that they work only on a subset 37 | of \code{.data}, this subset being defined by the \code{.condition}. 38 | 39 | The functions \code{mutate_which_} and \code{transmute_which_} are 40 | standard evaluation versions, similar to \code{\link[dplyr]{mutate_}} and 41 | \code{\link[dplyr]{transmute_}}. 42 | } 43 | \examples{ 44 | df <- mtcars[1:10,] 45 | 46 | # Non-standard evaluation 47 | mutate_which(df, gear==4, carb = 100) 48 | transmute_which(df, gear==4, carb = 100) 49 | 50 | # Standard evaluation 51 | mutate_which_(df, ~ gear==4, carb = ~ 100) 52 | transmute_which_(df, ~ gear==4, carb = ~ 100) 53 | 54 | } 55 | \seealso{ 56 | \code{\link[dplyr]{mutate}}, 57 | \code{\link[dplyr]{mutate_}}, 58 | \code{\link[dplyr]{transmute}}, 59 | \code{\link[dplyr]{transmute_}} from package \pkg{dplyr}. 60 | } 61 | \author{ 62 | Adapted from G. Grothendieck on StackOverflow, see 63 | \url{http://stackoverflow.com/a/34096575}. 64 | } 65 | -------------------------------------------------------------------------------- /man/pull.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pull.R 3 | \name{pull} 4 | \alias{pull} 5 | \alias{pull_} 6 | \alias{pull_.data.frame} 7 | \alias{pull_.matrix} 8 | \alias{pull_.list} 9 | \title{Column selection} 10 | \usage{ 11 | pull(.data, j) 12 | 13 | pull_(.data, j) 14 | 15 | \method{pull_}{data.frame}(.data, j) 16 | 17 | \method{pull_}{matrix}(.data, j) 18 | 19 | \method{pull_}{list}(.data, j) 20 | } 21 | \arguments{ 22 | \item{.data}{A tbl.} 23 | 24 | \item{j}{integer. The column to be extracted.} 25 | } 26 | \value{ 27 | A vector of length \code{nrow(.data)} 28 | } 29 | \description{ 30 | The function \code{pull} selects a column in a data frame 31 | and transforms it into a vector. 32 | This is useful to use it in combination with \pkg{magrittr}'s pipe operator 33 | and \pkg{dplyr}'s verbs. 34 | } 35 | \examples{ 36 | library(dplyr) 37 | mtcars[["mpg"]] 38 | mtcars \%>\% pull(mpg) 39 | 40 | # more convenient than (mtcars \%>\% filter(mpg > 20))[[3L]] 41 | mtcars \%>\% 42 | filter(mpg > 20) \%>\% 43 | pull(3) 44 | 45 | } 46 | \author{ 47 | Adapted from Tommy O' Dell, 48 | see \url{http://stackoverflow.com/a/24730843/3902976} on StackOverflow. 49 | } 50 | -------------------------------------------------------------------------------- /man/reexports.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/reexports_bazar.R, R/reexports_dplyr.R, 3 | % R/reexports_magrittr.R 4 | \docType{import} 5 | \name{reexports} 6 | \alias{reexports} 7 | \alias{concat} 8 | \alias{reexports} 9 | \alias{concat0} 10 | \alias{reexports} 11 | \alias{concat_} 12 | \alias{reexports} 13 | \alias{mutate} 14 | \alias{reexports} 15 | \alias{mutate_} 16 | \alias{reexports} 17 | \alias{rename} 18 | \alias{reexports} 19 | \alias{rename_} 20 | \alias{reexports} 21 | \alias{select} 22 | \alias{reexports} 23 | \alias{select_} 24 | \alias{reexports} 25 | \alias{transmute} 26 | \alias{reexports} 27 | \alias{transmute_} 28 | \alias{reexports} 29 | \alias{\%>\%} 30 | \title{Objects exported from other packages} 31 | \keyword{internal} 32 | \description{ 33 | These objects are imported from other packages. Follow the links 34 | below to see their documentation. 35 | 36 | \describe{ 37 | \item{bazar}{\code{\link[bazar]{concat}}, \code{\link[bazar]{concat0}}, \code{\link[bazar]{concat_}}} 38 | 39 | \item{dplyr}{\code{\link[dplyr]{mutate}}, \code{\link[dplyr]{mutate_}}, \code{\link[dplyr]{rename}}, \code{\link[dplyr]{rename_}}, \code{\link[dplyr]{select}}, \code{\link[dplyr]{select_}}, \code{\link[dplyr]{transmute}}, \code{\link[dplyr]{transmute_}}} 40 | 41 | \item{magrittr}{\code{\link[magrittr]{\%>\%}}} 42 | }} 43 | 44 | -------------------------------------------------------------------------------- /man/take.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/take.R 3 | \name{take} 4 | \alias{take} 5 | \alias{take_} 6 | \alias{take_.data.frame} 7 | \alias{take_.tbl_df} 8 | \title{Subset data frames} 9 | \usage{ 10 | take(.data, .condition, ...) 11 | 12 | take_(.data, .condition, ..., .dots) 13 | 14 | \method{take_}{data.frame}(.data, .condition, ..., .dots) 15 | 16 | \method{take_}{tbl_df}(.data, .condition, ..., .dots) 17 | } 18 | \arguments{ 19 | \item{.data}{A tbl or data.frame.} 20 | 21 | \item{.condition}{A condition defining the \code{\link[dplyr]{filter}} to be applied on 22 | \code{.data}.} 23 | 24 | \item{...}{Variable names to be \code{\link[dplyr]{select}}ed.} 25 | 26 | \item{.dots}{character vector of variable names to be \code{\link[dplyr]{select}}ed.} 27 | } 28 | \value{ 29 | A tbl or data.frame 30 | } 31 | \description{ 32 | Return subset of a data frame which meets conditions. 33 | } 34 | \examples{ 35 | df <- mtcars[1:10,] 36 | take(df, cyl \%in\% c(4, 6), mpg, disp) 37 | take_(df, ~ cyl \%in\% c(4, 6), ~ mpg, ~ disp) 38 | take_(df, ~ cyl \%in\% c(4, 6), .dots = c("mpg", "disp")) 39 | 40 | } 41 | \seealso{ 42 | \code{\link[dplyr]{filter}} and \code{\link[dplyr]{select}} 43 | from package \pkg{dplyr}. 44 | } 45 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | 2 | library(testthat) 3 | test_check("lplyr") 4 | -------------------------------------------------------------------------------- /tests/testthat/test-test.R: -------------------------------------------------------------------------------- 1 | context("Just a test of test") 2 | 3 | test_that("test", { 4 | x = 1L 5 | y = 2L 6 | expect_identical(x, y-x) 7 | }) 8 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R -------------------------------------------------------------------------------- /vignettes/lplyr-vignette.R: -------------------------------------------------------------------------------- 1 | ## ---- echo = FALSE, message = FALSE, warning = FALSE--------------------- 2 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>") 3 | library(dplyr) 4 | library(lplyr) 5 | 6 | ## ------------------------------------------------------------------------ 7 | xs <- list(x1 = 1:3, 8 | x2 = 2:5, 9 | x3 = "alpha") 10 | 11 | ## ---- eval = FALSE------------------------------------------------------- 12 | # mutate(xs, x4 = 4) %>% str 13 | # rename(xs, x0 = x1) %>% str 14 | 15 | ## ------------------------------------------------------------------------ 16 | mutate_(xs, x4 = ~ 4) %>% str 17 | rename_(xs, x0 = ~ x1) %>% str 18 | 19 | ## ------------------------------------------------------------------------ 20 | df <- mtcars[1:6,] 21 | mutate_which(df, gear==4, carb = 100) 22 | transmute_which(df, gear==4, carb = 100) 23 | 24 | ## ------------------------------------------------------------------------ 25 | mutate_which_(df, ~ gear==4, carb = ~ 100) 26 | transmute_which_(df, ~ gear==4, carb = ~ 100) 27 | 28 | ## ------------------------------------------------------------------------ 29 | df[["mpg"]] 30 | df %>% pull(mpg) 31 | 32 | # more convenient than (mtcars %>% filter(mpg > 20))[[3L]] 33 | df %>% 34 | filter(mpg > 20) %>% 35 | pull(3) 36 | 37 | -------------------------------------------------------------------------------- /vignettes/lplyr-vignette.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: 'Dplyr verbs for lists and other verbs for data frames' 3 | date: "`r Sys.Date()`" 4 | output: rmarkdown::html_vignette 5 | vignette: > 6 | %\VignetteIndexEntry{Dplyr verbs for lists and other verbs for data frames} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | \usepackage[utf8]{inputenc} 9 | --- 10 | 11 | ```{r, echo = FALSE, message = FALSE, warning = FALSE} 12 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>") 13 | library(dplyr) 14 | library(lplyr) 15 | ``` 16 | 17 | ## Verbs for lists and pairlists 18 | 19 | The package 'lplyr' extends some dplyr verbs to lists and pairlists: 20 | 21 | ```{r} 22 | xs <- list(x1 = 1:3, 23 | x2 = 2:5, 24 | x3 = "alpha") 25 | ``` 26 | 27 | ```{r, eval = FALSE} 28 | mutate(xs, x4 = 4) %>% str 29 | rename(xs, x0 = x1) %>% str 30 | ``` 31 | 32 | Usual verbs made for standard evaluation work as well: 33 | 34 | ```{r} 35 | mutate_(xs, x4 = ~ 4) %>% str 36 | rename_(xs, x0 = ~ x1) %>% str 37 | ``` 38 | 39 | 40 | ## New verbs for data frames 41 | 42 | The `mutate_which` and `transmute_which` functions are made for adding new variables or modifying existing ones on a subset of the data. 43 | 44 | ```{r} 45 | df <- mtcars[1:6,] 46 | mutate_which(df, gear==4, carb = 100) 47 | transmute_which(df, gear==4, carb = 100) 48 | ``` 49 | 50 | There is also a standard evaluation version of these functions, 51 | called `mutate_which_` and `transmute_which_`: 52 | 53 | ```{r} 54 | mutate_which_(df, ~ gear==4, carb = ~ 100) 55 | transmute_which_(df, ~ gear==4, carb = ~ 100) 56 | ``` 57 | 58 | The function `pull` selects a column in a data frame 59 | and transforms it into a vector. 60 | This is useful to use it in combination with 61 | magrittr's pipe operator and dplyr's verbs. 62 | 63 | ```{r} 64 | df[["mpg"]] 65 | df %>% pull(mpg) 66 | 67 | # more convenient than (mtcars %>% filter(mpg > 20))[[3L]] 68 | df %>% 69 | filter(mpg > 20) %>% 70 | pull(3) 71 | ``` 72 | -------------------------------------------------------------------------------- /vignettes/lplyr-vignette.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Dplyr verbs for lists and other verbs for data frames 17 | 18 | 19 | 20 | 21 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 |

Dplyr verbs for lists and other verbs for data frames

71 |

2017-11-04

72 | 73 | 74 | 75 |
76 |

Verbs for lists and pairlists

77 |

The package ‘lplyr’ extends some dplyr verbs to lists and pairlists:

78 |
xs <- list(x1 = 1:3, 
 79 |            x2 = 2:5, 
 80 |            x3 = "alpha")
81 |
mutate(xs, x4 = 4) %>% str
 82 | rename(xs, x0 = x1) %>% str
83 |

Usual verbs made for standard evaluation work as well:

84 |
mutate_(xs, x4 = ~ 4) %>% str
 85 | #> List of 4
 86 | #>  $ x1: int [1:3] 1 2 3
 87 | #>  $ x2: int [1:4] 2 3 4 5
 88 | #>  $ x3: chr "alpha"
 89 | #>  $ x4: num 4
 90 | rename_(xs, x0 = ~ x1) %>% str
 91 | #> List of 3
 92 | #>  $ x0: int [1:3] 1 2 3
 93 | #>  $ x2: int [1:4] 2 3 4 5
 94 | #>  $ x3: chr "alpha"
95 |
96 |
97 |

New verbs for data frames

98 |

The mutate_which and transmute_which functions are made for adding new variables or modifying existing ones on a subset of the data.

99 |
df <- mtcars[1:6,]
100 | mutate_which(df, gear==4, carb = 100)
101 | #>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
102 | #> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4  100
103 | #> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4  100
104 | #> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4  100
105 | #> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
106 | #> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
107 | #> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
108 | transmute_which(df, gear==4, carb = 100)
109 | #>                   carb
110 | #> Mazda RX4          100
111 | #> Mazda RX4 Wag      100
112 | #> Datsun 710         100
113 | #> Hornet 4 Drive       1
114 | #> Hornet Sportabout    2
115 | #> Valiant              1
116 |

There is also a standard evaluation version of these functions, called mutate_which_ and transmute_which_:

117 |
mutate_which_(df, ~ gear==4, carb = ~ 100)
118 | #>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
119 | #> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4  100
120 | #> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4  100
121 | #> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4  100
122 | #> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
123 | #> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
124 | #> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
125 | transmute_which_(df, ~ gear==4, carb = ~ 100)
126 | #>                   carb
127 | #> Mazda RX4          100
128 | #> Mazda RX4 Wag      100
129 | #> Datsun 710         100
130 | #> Hornet 4 Drive       1
131 | #> Hornet Sportabout    2
132 | #> Valiant              1
133 |

The function pull selects a column in a data frame and transforms it into a vector. This is useful to use it in combination with magrittr’s pipe operator and dplyr’s verbs.

134 |
df[["mpg"]]
135 | #> [1] 21.0 21.0 22.8 21.4 18.7 18.1
136 | df %>% pull(mpg)
137 | #> [1] 21.0 21.0 22.8 21.4 18.7 18.1
138 | 
139 | # more convenient than (mtcars %>% filter(mpg > 20))[[3L]]
140 | df %>%
141 |   filter(mpg > 20) %>%
142 |   pull(3)
143 | #> [1] 160 160 108 258
144 |
145 | 146 | 147 | 148 | 149 | 157 | 158 | 159 | 160 | --------------------------------------------------------------------------------