purrr

Harnessing .f list names with purrr::pmap

会有一股神秘感。 提交于 2019-11-27 22:34:53
The following works ok: pmap_dbl(iris, ~ ..1 + ..2 + ..3 + ..4) The documentation for .l provides for A list of lists. ... List names will be used if present. . This suggests you should be able to work with the list names (i.e. column names). However: pmap_dbl(iris, ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width) Error in .f(Sepal.Length = .l[[c(1L, i)]], Sepal.Width = .l[[c(2L, i)]], : object 'Sepal.Length' not found How are list names harnessed in practice? The formula argument ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width is passed to purrr::as_mapper . purrr::as

What is meaning of first tilde in purrr::map

五迷三道 提交于 2019-11-27 22:22:07
I was looking at this example that uses map . Here it is: mtcars %>% split(.$cyl) %>% # from base R map(~ lm(mpg ~ wt, data = .)) What is the meaning of the first tilde in map(~ lm... ? That is, how does R interpret the first tilde? (I understand that the second tilde indicates a function...) . Another way of asking is, why doesn't the following work? mtcars %>% split(.$cyl) %>% # from base R map(lm(mpg ~ wt, data = .)) As per the map help documentation , map needs a function but it also accepts a formula, character vector, numeric vector, or list, the latter of which are converted to

How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs

≡放荡痞女 提交于 2019-11-27 01:24:51
问题 I have to following issue using R. In short I want to create multiple new columns in a data frame based on calculations of different column pairs in the data frame. The data looks as follows: df <- data.frame(a1 = c(1:5), b1 = c(4:8), c1 = c(10:14), a2 = c(9:13), b2 = c(3:7), c2 = c(15:19)) df a1 b1 c1 a2 b2 c2 1 4 10 9 3 15 2 5 11 10 4 16 3 6 12 11 5 17 4 7 13 12 6 18 5 8 14 13 7 19 The output is supposed to look like the following: a1 b1 c1 a2 b2 c2 sum_a sum_b sum_c 1 4 10 9 3 15 10 7 25 2

using lm in list column to predict new values using purrr

送分小仙女□ 提交于 2019-11-26 21:03:37
问题 I am trying to add a column of predictions to a dataframe that has a list column that contains an lm model. I adopted some of the code from this post. I have made a toy example here: library(dplyr) library(purrr) library(tidyr) library(broom) set.seed(1234) exampleTable <- data.frame( ind = c(rep(1:5, 5)), dep = rnorm(25), groups = rep(LETTERS[1:5], each = 5) ) %>% group_by(groups) %>% nest(.key=the_data) %>% mutate(model = the_data %>% map(~lm(dep ~ ind, data = .))) %>% mutate(Pred = map2

Harnessing .f list names with purrr::pmap

那年仲夏 提交于 2019-11-26 16:40:40
问题 The following works ok: pmap_dbl(iris, ~ ..1 + ..2 + ..3 + ..4) The documentation for .l provides for A list of lists. ... List names will be used if present. . This suggests you should be able to work with the list names (i.e. column names). However: pmap_dbl(iris, ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width) Error in .f(Sepal.Length = .l[[c(1L, i)]], Sepal.Width = .l[[c(2L, i)]], : object 'Sepal.Length' not found How are list names harnessed in practice? 回答1: The formula

purrr map equivalent of nested for loop

心已入冬 提交于 2019-11-26 13:18:55
问题 What is the purrr::map equivalent of: for (i in 1:4) { for (j in 1:6) { print(paste(i, j, sep = "-")) } } OR lapply(1:4, function(i) lapply(1:6, function(j) print(paste(i, j, sep = "-")))) Conceptually, what I'm not getting is how to refer to the outer loop in the inner map function. map(1:4, ~ map(1:6, ~ print(paste(.x, ????, sep = "-"))) 回答1: As @r2evans points out, the .x from your first call is masked. however you can create a lambda function that takes 2 parameters .x and .y , and assign

What is meaning of first tilde in purrr::map

时光总嘲笑我的痴心妄想 提交于 2019-11-26 12:27:48
问题 I was looking at this example that uses map . Here it is: mtcars %>% split(.$cyl) %>% # from base R map(~ lm(mpg ~ wt, data = .)) What is the meaning of the first tilde in map(~ lm... ? That is, how does R interpret the first tilde? (I understand that the second tilde indicates a function...) . Another way of asking is, why doesn\'t the following work? mtcars %>% split(.$cyl) %>% # from base R map(lm(mpg ~ wt, data = .)) 回答1: As per the map help documentation, map needs a function but it also

Why use purrr::map instead of lapply?

不羁岁月 提交于 2019-11-26 10:05:19
问题 Is there any reason why I should use map(<list-like-object>, function(x) <do stuff>) instead of lapply(<list-like-object>, function(x) <do stuff>) the output should be the same and the benchmarks I made seem to show that lapply is slightly faster (it should be as map needs to evaluate all the non-standard-evaluation input). So is there any reason why for such simple cases I should actually consider switching to purrr::map ? I am not asking here about one\'s likes or dislikes about the syntax,