purrr

purrr pmap to read max column name by column name number

北战南征 提交于 2019-12-13 02:49:35
问题 I have this dataset: library(dpylr) Problem<- tibble(name = c("Angela", "Claire", "Justin", "Bob", "Gil"), status_1 = c("Registered", "No Action", "Completed", "Denied", "No Action"), status_2 = c("Withdrawn", "No Action", "Registered", "No Action", "Exempt"), status_3 = c("No Action", "Registered", "Withdrawn", "No Action", "No Action")) I want to make a column that has everyone's current status. If the person has ever completed the course, they are completed. If they were ever exempt, they

Store output from purrr:map_dfr and dplyr::group_split with while loop

主宰稳场 提交于 2019-12-12 18:22:16
问题 I would like to use map_dfr and group_split to run groups of a data.frame through a while loop and store the results. I can do this for one group like this. # df dput below # this code finds the closet match for DIFF for Sample.x in Sample.y, then finds the next closest match, until df_f <- df %>% filter(grp == "AB" & VAR == "Var1") HowMany <- length(unique(df_f$Sample.y)) i <- 1 MyList <- list() while (i <= HowMany){ res1 <- df_f %>% group_by(grp, VAR, Sample.x) %>% filter(DIFF == min(DIFF))

Using fct_relevel over a list of variables using map_at

為{幸葍}努か 提交于 2019-12-12 17:32:34
问题 I have a bunch of factor variables that have the same levels, and I want them all reordered similarly using fct_relevel from the forcats package. Many of the variable names start with the same characters ("Q11A" to "Q11X", "Q12A" to "Q12X", "Q13A" to "Q13X", etc.). I wanted to use the starts_with function from dplyr to shorten the task. The following error didn't give me an error, but it didn't do anything either. Is there anything I'm doing wrong? library(dplyr) library(purrr) library

Use purrr() to rather than lapply for arbitrary city/county pairs in tidycensus?

时光毁灭记忆、已成空白 提交于 2019-12-12 17:17:59
问题 I've got a giant lapply running to download a file of data. But it seems clumsy. But mapply does not seem right, as I don't want all state/county combinations. I hear good things about map(). Can anyone furnish an example of how I might use purrr() command 'map' for the following code? library(tidycensus) library(sf) mykey<-"youhavetogetyourownimafraid" #variables to test out the function############# x<-"06" y<-"073" z<-"2000" setwd("N:/Dropbox/_BonesFirst/149_Transit_Metros_BG_StateSplit_by

Processing arbitrary hierarchies recursively with purrr

↘锁芯ラ 提交于 2019-12-12 12:58:20
问题 Suppose that I wanted to prune a tree consisting of a hierarchy of nested lists in R, based on some particular criterion. I can do this "easily" enough using lapply : # Based an example from the NetworkD3 documentation # https://christophergandrud.github.io/networkD3/ URL <- paste0( "https://cdn.rawgit.com/christophergandrud/networkD3/", "master/JSONdata//flare.json") flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE) # Leaf nodes have a "size" attribute. Let's say we want to # prune

purrr:map and glm - issues with call

浪子不回头ぞ 提交于 2019-12-12 09:58:33
问题 This issue is related to Pipe '.' dot causes trouble in glm call. purrr:map is wonderful for subgroup analysis and/or model comparison. However, when using glm , the call is messed up and causing issues, e.g. when computing pseudo-R2s. The reason is that update doesn't work with the ugly call , and thus pscl::pR2 cannot compute the log-likelihood of the base model. pacman::p_load(tidyverse) #sample data pacman::p_load(ISLR) mydata = ISLR::Default #nest data, students and non-students Default

Using purrr (tidyverse) to map distance function across all columns of dataframe

我与影子孤独终老i 提交于 2019-12-12 04:18:42
问题 I have a distance function which takes in 2 (numeric) vectors and calculates the distance between them. For a given dataframe ( mtcars_raw ) in the example below and a fixed input vector ( test_vec ) I would like to calculate the pairwise distances (i.e. apply the distance function) to each column and test_vec and return the vector of distances. The length of the vector should be the number of columns. Please see the reproducible example: library(datasets) # The raw dataframe containing only

purrr pmap command to find lessons that have following lessons

和自甴很熟 提交于 2019-12-11 19:32:20
问题 I have a dataset of school classes: students <- structure(list(Name = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "T", "T", "T", "T", "T", "T", "T"), Week = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0), Day = c("Friday", "Friday", "Thursday", "Thursday", "Tuesday", "Wednesday", "Wednesday", "Friday", "Thursday", "Thursday", "Tuesday", "Tuesday", "Friday", "Friday", "Friday", "Thursday", "Thursday", "Tuesday", "Wednesday" ), Start = c("09:15", "11:35", "09:15",

gmailR - send multiple emails through R with attachment

纵然是瞬间 提交于 2019-12-11 19:04:28
问题 I have figured out how to sent a single email through R with an attachment and have the body of the email show up (seems to be a common problem). However, I'd like to follow Jenny Bryan's method of sending multiple emails while also attaching a file. Sending a single email with attachment and a message. msg <- "this is the message of the email" test_email <- mime() %>% to("to@gmail.com") %>% from("from@gmail") %>% subject("subject goes here") %>% body(msg) %>% attach_file("29697.html", type =

How to efficiently import multiple excel tables located in one sheet into an R list?

ε祈祈猫儿з 提交于 2019-12-11 18:46:07
问题 Task I am trying to import tables situated in a single excel sheet into an R object as efficiently as possible ( list will be fine, as I can take the rest of the calculations from there). Nuance The tables are actually excel ranges not excel tables , but they are structured and look like tables: here is an example of an excel range that should be imported as a table in R: Ranges(In a table form) are not of the same length and can be situated anywhere in the same sheet. Reproducible Example