mutate_elem: Add new variables

Description Usage Arguments See Also Examples

Description

Add new variables

Usage

1
2
3
4
5
6
7
8
9
mutate_elem(.data, ...)

transmute_elem(.data, ...)

## S3 method for class 'list'
mutate(.data, ..., gather = unlist_with_nas, warn = FALSE)

## S3 method for class 'list'
transmute(.data, ..., gather = unlist_with_nas, warn = FALSE)

Arguments

.data

a list.

...

name-value pairs of expressions.

mutate_elem and transmute_elem work with data taking as input one element at a time, they can be used to transform individual elements of the list. mutate and transmute work in a similar manner as their dplyr counterparts, i.e. they are applied to whole lists/vectors.

Notice: in mutate or transmute expressions need to evaluate to vectors or lists of the same length as length(.data), otherwise the functions will throw an error.

gather

before calculating the summaries, each variable is first passed through the gather function. Common choices for gather are unlist, or unlist_with_nas. If data is to be taken as-is, use identity function.

warn

if FALSE (default) it ignores the warnings throwed by purrr's transpose.

See Also

mutate, transmute

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
lst <- list(
  list(a = 1:3),
  list(a = 5),
  list(a = c(NA, 4)),
  list(a = NA),
  list(a = 100)
)

lst %>% mutate_elem(norm_a = a/sum(a, na.rm = TRUE))


as_lol(mtcars) %>%
  transmute(
    avg_mpg = rep_list(mean(mpg), length(mpg)),
    cyl = paste("it has", cyl, "cylinders")
  ) %>% .[1:5]

as_lol(mtcars) %>%
  mutate(
    avg_mpg = rep_list(mean(mpg), length(mpg)),
    cyl = cyl + 1000
  ) %>% .[1:5]

list(
  a = list(x = 1),
  b = list(x = 2),
  c = list(x = 3),
  list(x = 4)
 ) %>%
  mutate(y = x / sum(x))

twolodzko/lolplyr documentation built on May 14, 2019, 8:22 a.m.