purrr

when to use map() function and when to use summarise_at()/mutate_at()

纵饮孤独 提交于 2020-08-18 17:19:24
问题 Can anyone give a suggestion regarding when to use the map() (all map_..() functions) and when to use summarise_at() / mutate_at() ? E.g. if we are doing some modification to the column of vectors then we do not need to think map() ? If we have a df / have a column has a list in it then we need to use map() ? Does map() function always need to be used with nest() function? Anyone could suggest some learning videos regarding this. And also how to put lists in df and modeling multiple lists at

when to use map() function and when to use summarise_at()/mutate_at()

杀马特。学长 韩版系。学妹 提交于 2020-08-18 17:16:10
问题 Can anyone give a suggestion regarding when to use the map() (all map_..() functions) and when to use summarise_at() / mutate_at() ? E.g. if we are doing some modification to the column of vectors then we do not need to think map() ? If we have a df / have a column has a list in it then we need to use map() ? Does map() function always need to be used with nest() function? Anyone could suggest some learning videos regarding this. And also how to put lists in df and modeling multiple lists at

How to compute multiple new columns in a R dataframe with dynamic names

你。 提交于 2020-08-09 06:15:54
问题 I'm trying to generate multiple new columns/variables in a R dataframe with dynamic new names taken from a vector. The new variables are computed from groups/levels of a single column. The dataframe contains measurements ( counts ) of different chemical elements ( element ) along depth ( z ). The new variables are computed by diving the counts of each element at a certain depth by the respective counts of proxy elements ( proxies ) at the same depth. There is already a solution using mutate

Tidyverse approach to binding unnamed list of unnamed vectors by row - do.call(rbind,x) equivalent

左心房为你撑大大i 提交于 2020-07-28 14:16:26
问题 I often find questions where people have somehow ended up with an unnamed list of unnamed character vectors and they want to bind them row-wise into a data.frame . Here is an example: library(magrittr) data <- cbind(LETTERS[1:3],1:3,4:6,7:9,c(12,15,18)) %>% split(1:3) %>% unname data #[[1]] #[1] "A" "1" "4" "7" "12" # #[[2]] #[1] "B" "2" "5" "8" "15" # #[[3]] #[1] "C" "3" "6" "9" "18" One typical approach is with do.call from base R. do.call(rbind, data) %>% as.data.frame # V1 V2 V3 V4 V5 #1

Tidyverse approach to binding unnamed list of unnamed vectors by row - do.call(rbind,x) equivalent

女生的网名这么多〃 提交于 2020-07-28 14:14:31
问题 I often find questions where people have somehow ended up with an unnamed list of unnamed character vectors and they want to bind them row-wise into a data.frame . Here is an example: library(magrittr) data <- cbind(LETTERS[1:3],1:3,4:6,7:9,c(12,15,18)) %>% split(1:3) %>% unname data #[[1]] #[1] "A" "1" "4" "7" "12" # #[[2]] #[1] "B" "2" "5" "8" "15" # #[[3]] #[1] "C" "3" "6" "9" "18" One typical approach is with do.call from base R. do.call(rbind, data) %>% as.data.frame # V1 V2 V3 V4 V5 #1

Creating a dynamic Group By

吃可爱长大的小学妹 提交于 2020-07-05 04:39:05
问题 df = data.frame( A = c(1, 4, 5, 13, 2), B = c("Group 1", "Group 3", "Group 2", "Group 1", "Group 2"), C = c("Group 3", "Group 2", "Group 1", "Group 2", "Group 3") ) df %>% group_by(B) %>% summarise(val = mean(A)) df %>% group_by(C) %>% summarise(val = mean(A)) Instead of writing a new chunck of code for each unique set of group_by I would like to create a loop that would iterate through the df data frame and save the results into a list or a data frame. I would like to see how the average

R: Forecasting multiple time series with fable, tsibble and map

与世无争的帅哥 提交于 2020-06-29 06:50:29
问题 I am trying to fit some time series using the R packages tsibble and fable , the still-under-construction replacement for the redoubtable Rob Hyndman's forecast package. The series are all combined into one tsibble, which I then fit with ARIMA, a function which replaces, among other things, forecast::auto.arima . I use map_at , first to iterate over all the elements except the Date , and then again to extract the model information from the models that have been fit to each series using

R: Forecasting multiple time series with fable, tsibble and map

我的梦境 提交于 2020-06-29 06:50:01
问题 I am trying to fit some time series using the R packages tsibble and fable , the still-under-construction replacement for the redoubtable Rob Hyndman's forecast package. The series are all combined into one tsibble, which I then fit with ARIMA, a function which replaces, among other things, forecast::auto.arima . I use map_at , first to iterate over all the elements except the Date , and then again to extract the model information from the models that have been fit to each series using

Use purrr:map with ggplot

妖精的绣舞 提交于 2020-06-29 04:04:32
问题 I don't have much experience using the purrr package. I have a dataframe named data which looks like this: Country Year Incidence USA 1995 20000 USA 2000 23000 UK 1995 16000 UK 2000 22000 It's confidential and I can't share it so this is just a small excrept. I need to make a plot where Year is on x-axis and incidence on y-axis, however, I need to have separate plots for each country. Faceting is unfortunately not an option, I need to save each plot as a separate file. I know how I would

How to reference a column in a nested dataframe (then use purrr::map)

喜夏-厌秋 提交于 2020-06-27 04:15:11
问题 I have a very simple question about referencing data columns within a nested dataframe. For a reproducible example, I'll nest mtcars by the two values of variable am : library(tidyverse) mtcars_nested <- mtcars %>% group_by(am) %>% nest() mtcars_nested which gives data that looks like this. #> # A tibble: 2 x 2 #> # Groups: am [2] #> am data #> <dbl> <list> #> 1 1 <tibble [13 × 10]> #> 2 0 <tibble [19 × 10]> If I now wanted to use purrr::map to take the mean of mpg for each level of am I