purrr

replacing `.x` with column name after running `map::purrr()` function

蓝咒 提交于 2019-12-01 11:33:43
问题 I run lm() for every column of a dataset with one of the column as the dependent variable, using purrr:map() function. The results are almost perfect except for this - I want to replace .x in the results with the variable that i run lm() for. The post R purrr map show column names in output is related but I want to avoid creating a function. Below, are the codes using the mtcars dataset. I know, for example that .x for the first output refers to $mpg . I am not sure if setNames() will work.

How to use purrr's map function to perform row-wise prop.tests and add results to the dataframe?

こ雲淡風輕ζ 提交于 2019-12-01 01:41:22
I'm trying to solve the following problem in R: I have a dataframe with two variables (number of successes, and number of total trials). # A tibble: 4 x 2 Success N <dbl> <dbl> 1 28. 40. 2 12. 40. 3 22. 40. 4 8. 40. I would like to perform a prop.test or binom.test on each row and add the resulting list to the dataframe (or certain elements of it, like the p-value and CIs). Ideally, I would like to add a third column with the p-values and the CI-range. My attempts so far were painly unsuccessful. Here is a minimal coding example: Success <- c( 38, 12, 27, 9) N <- c( 50, 50, 50, 50) df <- as

How to use walk to silently plot ggplot2 output with purrr

巧了我就是萌 提交于 2019-12-01 01:09:15
问题 I am trying to understand how to use walk to silently (without printing to the console) return ggplot2 plots in a pipeline. library(tidyverse) # EX1: This works, but prints [[1]], [[2]], ..., [[10]] to the console 10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map(~ data.frame(.x)) %>% map(~ ggplot(., aes(x, y)) + geom_point()) # EX2: This does not plot nor print anything to the console 10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map(~ data.frame(.x)) %>% walk(~ ggplot(., aes(x, y)) + geom

Parse Error: “Trailing Garbage” while trying to parse JSON column in data frame

时光怂恿深爱的人放手 提交于 2019-11-30 18:59:37
I have a log file that look like this . I'm trying to parse the JSON in the Message column by: library(readr) library(jsonlite) df <- read_csv("log_file_from_above.csv") fromJSON(as.character(df$Message)) But, I'm hitting the following error: Error: parse error: trailing garbage "isEmailConfirmed": false } { "id": -1, "firstName": (right here) ------^ How can I get rid of the "trailing garbage"? fromJSON() isn't "apply"ing against the character vector, it's trying to convert it all to a data frame. You can try purrr::map(df$Message, jsonlite::fromJSON) what @Abdou provided or jsonlite::stream

Using purrr::pmap within mutate to create list-column

僤鯓⒐⒋嵵緔 提交于 2019-11-30 18:04:06
I understand how to use map to iterate over arguments in a df and create a new list column. For example, params <- expand.grid(param_a = c(2, 4, 6) ,param_b = c(3, 6, 9) ,param_c = c(50, 100) ,param_d = c(1, 0) ) df.preprocessed <- dplyr::as.tbl(params) %>% dplyr::mutate(test_var = purrr::map(param_a, function(x){ rep(5, x) } )) However, how do I use the analogous syntax with pmap in the event that I want to specify more than 2 parameters? df.preprocessed <- dplyr::as.tbl(params) %>% dplyr::mutate(test_var = purrr::pmap(list(x = param_a ,y = param_b ,z = param_c ,u = param_d), function(x, y){

Pass function arguments by column position to mutate_at

一世执手 提交于 2019-11-30 17:51:21
I'm trying to tighten up a %>% piped workflow where I need to apply the same function to several columns but with one argument changed each time. I feel like purrr 's map or invoke functions should help, but I can't wrap my head around it. My data frame has columns for life expectancy, poverty rate, and median household income. I can pass all these column names to vars in mutate_at , use round as the function to apply to each, and optionally supply a digits argument. But I can't figure out a way, if one exists, to pass different values for digits associated with each column. I'd like life

Add titles to ggplots created with map()

﹥>﹥吖頭↗ 提交于 2019-11-30 15:01:02
What's the easiest way to add titles to each ggplot that I've created below using the map function? I want the titles to reflect the name of each data frame - i.e. 4, 6, 8 (cylinders). Thanks :) mtcars_split <- mtcars %>% split(mtcars$cyl) plots <- mtcars_split %>% map(~ ggplot(data=.,mapping = aes(y=mpg,x=wt)) + geom_jitter() # + ggtitle(....)) plots Use map2 with names . plots <- map2( mtcars_split, names(mtcars_split), ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + geom_jitter() + ggtitle(.y) ) Edit: alistaire pointed out this is the same as imap plots <- imap( mtcars_split, ~ggplot

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

折月煮酒 提交于 2019-11-30 10:36:27
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.74354377676955), .Names = c("id", "label", "link", "score", "externalId")), structure(list( id = 9L, label

Parse Error: “Trailing Garbage” while trying to parse JSON column in data frame

偶尔善良 提交于 2019-11-30 03:19:09
问题 I have a log file that look like this. I'm trying to parse the JSON in the Message column by: library(readr) library(jsonlite) df <- read_csv("log_file_from_above.csv") fromJSON(as.character(df$Message)) But, I'm hitting the following error: Error: parse error: trailing garbage "isEmailConfirmed": false } { "id": -1, "firstName": (right here) ------^ How can I get rid of the "trailing garbage"? 回答1: fromJSON() isn't "apply"ing against the character vector, it's trying to convert it all to a

Pass function arguments by column position to mutate_at

徘徊边缘 提交于 2019-11-30 01:50:20
问题 I'm trying to tighten up a %>% piped workflow where I need to apply the same function to several columns but with one argument changed each time. I feel like purrr 's map or invoke functions should help, but I can't wrap my head around it. My data frame has columns for life expectancy, poverty rate, and median household income. I can pass all these column names to vars in mutate_at , use round as the function to apply to each, and optionally supply a digits argument. But I can't figure out a