purrr

How to take subsets of lists in a tibble

北战南征 提交于 2020-01-05 04:40:13
问题 I have annual financial data for several stocks. I needed to blow it out to become monthly data and, thanks to an answer to this question I'd asked earlier, I have a solution which involves mutating the date column into lists of dates : library(tidyverse) library(lubridate) factors.subset.raw = structure(list( sec_id = c(1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1572L, 1676L, 1676L, 1676L,

R - Composite functions vs piped functions in `purrr::map()`

三世轮回 提交于 2020-01-05 03:37:48
问题 I have the following list: my_list = list(alpha = list('hi'), beta = list(1:4, 'how'), gamma = list(TRUE, FALSE, 'are')) str(my_list) List of 3 $ alpha:List of 1 ..$ : chr "hi" $ beta :List of 2 ..$ : int [1:4] 1 2 3 4 ..$ : chr "how" $ gamma:List of 3 ..$ : logi TRUE ..$ : logi FALSE ..$ : chr "are" I would like to figure out which data types are contained within each level 1 element. To do this, I can use the following pipeline: piped = map(my_list, ~map(., class) %>% unique %>% unlist) str

How can I use purrr to match records from a lookup table?

送分小仙女□ 提交于 2020-01-04 05:33:30
问题 I have this dataset library(dplyr) data_frame(Q1= c('AL', NA, 'TX', 'FL'), Q2=c('MN', 'CO', NA, NA), value=c(10,24,12,54)) # A tibble: 4 x 3 Q1 Q2 value <chr> <chr> <dbl> 1 AL MN 10 2 <NA> CO 24 3 TX <NA> 12 4 FL <NA> 54 And I am trying to use purrr to convert the values in Q1 and Q2 into full state names using a lookup table lktState <- data_frame(abb=state.abb, name=state.name) So far I've tried this but it doesn't work data_frame(Q1= c('AL', NA, 'TX', 'FL'), Q2=c('MN', 'CO', NA, NA), value

Dplyr select_ and starts_with on multiple values in a variable list part 2

ぐ巨炮叔叔 提交于 2020-01-03 05:25:06
问题 This is a continuation from my question earlier: Dplyr select_ and starts_with on multiple values in a variable list I am collecting data from differnt sensors in various locations, data output is something like: df<-data.frame(date=c(2011,2012,2013,2014,2015),"Sensor1 Temp"=c(15,18,15,14,19),"Sensor1 Pressure"=c(1001, 1000, 1002, 1004, 1000),"Sensor1a Temp"=c(15,18,15,14,19),"Sensor1a Pressure"=c(1001, 1000, 1002, 1004, 1000), "Sensor2 Temp"=c(15,18,15,14,19),"Sensor2 Pressure"=c(1001, 1000,

iterating over formulas in purrr

妖精的绣舞 提交于 2020-01-03 05:06:46
问题 I have a bunch of formulas, as strings, that I'd like to use, one at a time in a glm, preferably using tidyverse functions. Here's where I am at now. library(tidyverse) library(broom) mtcars %>% dplyr::select(mpg:qsec) %>% colnames -> targcols paste('vs ~ ', targcols) -> formulas formulas #> 'vs ~ mpg' 'vs ~ cyl' 'vs ~ disp' 'vs ~ hp' 'vs ~ drat' 'vs ~ wt' 'vs ~ qsec' I can run a general linear model with any one of these formulas as glm(as.formula(formulas[1]), family = 'binomial', data =

Add multiple output variables using purrr and a predefined function

前提是你 提交于 2020-01-02 03:35:48
问题 Take this simple dataset and function (representative of more complex problems): x <- data.frame(a = 1:3, b = 2:4) mult <- function(a,b,n) (a + b) * n Using base R's Map I could do this to add 2 new columns in a vectorised fashion: ns <- 1:2 x[paste0("new",seq_along(ns))] <- Map(mult, x["a"], x["b"], n=ns) x # a b new1 new2 #1 1 2 3 6 #2 2 3 5 10 #3 3 4 7 14 purrr attempt via pmap gets close with a list output: library(purrr) library(dplyr) x %>% select(a,b) %>% pmap(mult, n=1:2) #[[1]] #[1]

using purrr to affect single columns of each dataframe in a list

余生长醉 提交于 2020-01-02 02:03:37
问题 still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. All I want to do is convert the datetimes in the below, to dates with as.Date(). it's a list of dataframes. Been playing around but haven't found something that works yet... any help appreciated. df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"), useless = "ignore me") df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1),

using purrr to affect single columns of each dataframe in a list

耗尽温柔 提交于 2020-01-02 02:03:32
问题 still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. All I want to do is convert the datetimes in the below, to dates with as.Date(). it's a list of dataframes. Been playing around but haven't found something that works yet... any help appreciated. df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"), useless = "ignore me") df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1),

bunch recoding of variables in the tidyverse (functional / meta-programing)

血红的双手。 提交于 2020-01-01 19:39:11
问题 I want to recode a bunch of variables with as few function calls as possible. I have one data.frame where I want to recode a number of variables. I create a named list of all variable names and the recoding arguments I want to execute. Here I have no problem using map and dpylr . However, when it comes to recoding I find it much easier using recode from the car package, instead of dpylr 's own recoding function. A side question is whether there is a nice way of doing the same thing with dplyr

Updating a linear regression model with update and purrr

江枫思渺然 提交于 2020-01-01 06:30:07
问题 I want to update a lm -model using the update -function inside a map -call, but this throws the following error: mtcars %>% group_by(cyl) %>% nest() %>% mutate(lm1 = map(data, ~lm(mpg ~ wt, data = .x)), lm2 = map(lm1, ~update(object = .x, formula = .~ . + hp))) Error in mutate_impl(.data, dots) : Evaluation error: cannot coerce class ""lm"" to a data.frame. Can anyone help me with this problem? I am confused about this error, because e.g. this works totally fine: mtcars %>% group_by(cyl) %>%