purrr

Passing labels to xlab and ylab in ggplot2

╄→гoц情女王★ 提交于 2019-12-20 02:45:21
问题 I have created a function where the objective is to create a series of plots in a vectorzation way. The functions partially does what I want which is update update the plot based on the selected variables. However, I am not able to pass the label argument (i.e. label_x and label_y) so that the xlab and ylab are updated consistently. library(tidyverse) plot_scatter_with_label <- function(df, var_x, var_y, label_x, label_y, geom_smooth = FALSE, point_shape = 16, point_color = "#EB3300", point

Extracting data from a list of lists into its own `data.frame` with `purrr`

眉间皱痕 提交于 2019-12-18 13:49:14
问题 Representative sample data (list of lists): l <- list(structure(list(a = -1.54676469632688, b = "s", c = "T", d = structure(list(id = 5L, label = "Utah", link = "Asia/Anadyr", score = -0.21104594634643), .Names = c("id", "label", "link", "score")), e = 49.1279871269422), .Names = c("a", "b", "c", "d", "e")), structure(list(a = -0.934821052832427, b = "k", c = "T", d = list(structure(list(id = 8L, label = "South Carolina", link = "Pacific/Wallis", score = 0.526540892113734, externalId = -6

Add new variable to list of data frames with purrr and mutate() from dplyr

那年仲夏 提交于 2019-12-18 02:47:45
问题 I know that there are many related questions here on SO, but I am looking for a purrr solution, please, not one from the apply list of functions or cbind/rbdind (I want to take this opportunity to get to know purrr better). I have a list of dataframes and I would like to add a new column to each dataframe in the list. The value of the column will be the name of the dataframe, i.e. the name of each element in the list. There is something similar here, but it involves the use of a function and

Fit model using each predictor columns indiviually store results in dataframe

孤人 提交于 2019-12-14 03:49:09
问题 I have a dataframe with one column of a response variable, and several columns of predictor variables. I want to fit models for the response variable using each of the predictor variables separately, finally creating a dataframe that contains the coefficients of the model. Previously, I would have done this: data(iris) iris_vars <- c("Sepal.Width", "Petal.Length", "Petal.Width") fits.iris <- lapply(iris_vars, function(x) {lm(substitute(Sepal.Length ~ i, list(i = as.name(x))), data = iris)}) #

Combination of purrr::map and dplyr give inconsistent result with a plain statistical test

一曲冷凌霜 提交于 2019-12-14 02:08:53
问题 I'm comparing two vectors (data_A_score, data_B_score) with another vector K1, using ks.test(), which I get this result: score_ref_k1 <- c(0.09651, 0.09543, 0.09122, 0.09458, 0.09382, 0.10158, 0.10339, 0.13594, 0.09458, 0.09296) data_A_score_src <- c(0.09293, 0.09838, 0.09866, 0.10866, 0.09726, 0.10731, 0.09866, 0.09398, 0.10007, 0.10408) data_B_score_src <- c(0.04741, 0.0621, 0.09606, 0.08851, 0.05063, 0.39775, 0.05509, 0.10784, 0.0468, 0.04782) ks.test(data_A_score_src, score_ref_k1, exact

purrr::possibly function possibly not working with map2_chr function

守給你的承諾、 提交于 2019-12-13 17:02:04
问题 I suspect that this is a bug in the purrr package, but would like to check my logic in StackOverflow first, please. It seems to me that the possibly function is not working inside the map2_chr function. I'm using purrr version 0.2.5 Consider this example: library(dplyr) library(purrr) lets <- tibble(posn = 2:0, lets_list = list(letters[1:5], letters[1:5], letters[1:5])) %>% glimpse() returns Observations: 3 Variables: 2 $ posn <int> 2, 1, 0 $ lets_list <list> [<"a", "b", "c", "d", "e">, <"a",

How to most efficiently filter a dataframe conditionnaly of values in another one, in the tidyverse framework?

孤街醉人 提交于 2019-12-13 04:12:01
问题 I have a dataframe df1 with an ID column and a lubridate time interval column, and I want to filter (subsample) a dataframe df2, which has ID and DateTime columns, so that only df2 rows with DateTime fitting the corresponding ID interval in df1 are kept. I want to do so in a tidyverse framework. It can easily be done using a join (see example below), but I would like to know whether there would be a more direct solution (maybe purrr-based) that would avoid joining and then removing the time

Factors not being recognised in a lm using map()

谁说胖子不能爱 提交于 2019-12-13 03:07:25
问题 Aim: I want to use the map() function to do a linear model that involves categorical variables. Problem: I am getting the following error but I know the categorical variable included, borrower_genders has five levels. Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels What I've done: The following code works perfectly fine when removing borrower_genders , not with it, why? kiva_country%>% dplyr::group_by(country

assigning `pmap` output to dataframes with pattern names

試著忘記壹切 提交于 2019-12-13 02:58:05
问题 I am using pmap to run the same function across multiple dataframes and wish to assign each element of the output list to a separate object with a name that has a pattern. But I can't figure out how to do this. For example, here is a minimal example where I am computing quantiles for three different variables- # function call purrr::pmap(.l = list( x = list(iris$Sepal.Length, mtcars$wt, anscombe$y4), probs = list(seq(0, 1, 0.10)), na.rm = list(TRUE) ), .f = stats::quantile) # output #> [[1]]

R Scrape a list of Google + urls using purrr package

淺唱寂寞╮ 提交于 2019-12-13 02:54:34
问题 I am working on a web scraping project, which aims to extract Google + reviews from a set of children's hospitals. My methodology is as follows: 1) Define a list of Google + urls to navigate to for review scraping. The urls are in a dataframe along with other variables defining the hospital. 2) Scrape reviews, number of stars, and post time for all reviews related to a given url. 3) Save these elements in a dataframe, and name the dataframe after another variable in the dataframe