purrr

purrr: How to intersect one list with several nested lists

六眼飞鱼酱① 提交于 2019-12-11 18:12:06
问题 I have a data.frame containing parties in government. These parties are nested in a list column grouped by period (= each year). I want to compare the overlap between each government and previous governments. library(tidyverse) df <- tibble::tribble( ~period, ~party, ~seats, 1, "A", 2, 1, "B", 3, 1, "C", 3, 2, "A", 2, 2, "C", 3, 3, "C", 4, 3, "E", 1, 3, "F", 3 ) df1 <- df %>% group_by(period) %>% nest() %>% mutate(gov=map(data, "party") %>% map(.,list)) %>% mutate(prev.govs=map(data, "party")

purrr apply functions to lists in data.frame over specifed dimensions

蹲街弑〆低调 提交于 2019-12-11 17:53:29
问题 The following produces a data frame with <int[]> and <list[]> fields: library(tidyverse) set.seed(123) s <- 4 data <- data.frame( lamda = c(5, 2, 3), meanlog = c(9, 10, 11), sdlog = c(2, 2.1, 2.2) ) data2 <- data %>% mutate( freq = map(lamda, ~rpois(s, .x)), freqsev = map(freq, ~map(.x, function(k) rlnorm(k, meanlog, sdlog))) ) Output: as_tibble(data2) lamda meanlog sdlog freq freqsev <dbl> <dbl> <dbl> <list> <list> 1 5 9 2 <int [4]> <list [4]> 2 2 10 2.1 <int [4]> <list [4]> 3 3 11 2.2 <int

R - web scraping through multiple URLs? with rvest and purrr

旧城冷巷雨未停 提交于 2019-12-11 16:12:39
问题 I am trying to scrape football(soccer) statistics for a project i'm working on and i'm trying to utilise rvest and purrr to loop through the numeric values at the end of the url. I'm not sure what i'm missing but i have a snippet of the code as well as the error message that keeps coming up. library(xml2) library(rvest) library(purrr) wins_URL <- "https://www.premierleague.com/stats/top/clubs/wins?se=%d" map_df(1:15, function(i){ cat(".") page <- read_html(sprintf(wins_URL, i)) data.frame

Apply timeseries decomposition (and anomaly detection) over a sliding/tiled window

依然范特西╮ 提交于 2019-12-11 15:39:27
问题 Anomaly detection methods published and now abandoned by twitter have been separately forked and maintained in the anomalize package and the hrbrmstr/AnomalyDetection fork. Both have implemented features that are 'tidy'. Working static versions tidyverse_cran_downloads %>% filter(package == "tidyr") %>% ungroup() %>% select(-package) -> one_package_only one_package_only %>% anomalize::time_decompose(count, merge = TRUE, method = "twitter", frequency = "7 days") -> one_package_only_decomp one

Using the pipe in selfmade function with tidyeval (quo_name)

我是研究僧i 提交于 2019-12-11 15:17:32
问题 I have two functions: date_diff and group_stat. So I have read this article tidyverse and I try so create simple functions and use the pipe. The first function creates a difftime and names them timex_minus_timey but when I pipe this result into the next function I have to look at the name so I can fill in summary_var. Is there a better way to do this? library(tidyverse) # set.seed(42) data <- dplyr::bind_rows( tibble::tibble(Hosp = rep("A", 1000), drg = sample(letters[1:5], 1000, replace =

R officer - Nest Dataframe Into Grouped List And Export Tables to Word With Group Headers

跟風遠走 提交于 2019-12-11 15:14:34
问题 I read a similar question that helped me get to the point I'm at now, but am struggling with the next step. I have a dataframe similar to below - Product = c("Apple", "Apple", "Banana", "Banana", "Banana", "Carrot", "Carrot") Category = c(1, 2, 1, 2, 3, 1, 2) Slope = c(2.988, 2.311, 2.181, 6.387, 2.615, 7.936, 3.267) df = data.frame(Product, Category, Slope) My objective is to have a Word report with a table for each product. To do this, I create a list with the data and flextables, as below

Use R dplyr/purrr To Get Chi-square Output Matrices By Group

最后都变了- 提交于 2019-12-11 15:05:06
问题 I'd like to get chi-square output matrices (e.g., standardized residuals, expected values) by group using elements of the tidyverse. Using the mtcars data set, here's where I've started: mtcars %>% dplyr::select(vs, am) %>% table() %>% chisq.test(.) Which produces the chi-square test statistic. In order to get standardized residuals, for example, my only successful code is this: mtcars %>% dplyr::select(vs, am) %>% table() %>% chisq.test(.) -> chi.out chi.out$stdres vs am Freq 1 0 0 0.9523038

Can I apply R standard deviation across rows without `apply()` function? [duplicate]

烂漫一生 提交于 2019-12-11 14:55:34
问题 This question already has answers here : R/tidyverse: calculating standard deviation across rows (5 answers) Calculating standard deviation of each row (1 answer) Closed 8 months ago . library(tidyverse) df <- tibble(col1 = c(5, 2), col2 = c(6, 4), col3 = c(9, 9)) # # A tibble: 2 x 3 # col1 col2 col3 # <dbl> <dbl> <dbl> # 1 5 6 9 # 2 2 4 9 df %>% mutate(col4 = apply(.[, c(1, 3)], 1, sum)) df %>% mutate(col4 = rowSums(.[c(1, 3)], na.rm = TRUE)) Lately R's apply() function has been trouble for

using purr with expand.grid to loop over formulas for a t.test while conditioning on another variable

混江龙づ霸主 提交于 2019-12-11 13:40:54
问题 I'd like to make some purrr code more concise. I have a df with one dependent variable (y) and 4 independent variables (x1, x2, x3, x4). I also have one conditioning variable that takes 2 levels (z, is either zero or 1). I'd like to run 8 t-tests: y ~ x1 [z==0], y ~ x2 [z==0] ... y ~ x1 [z==1], y ~ x2 [z==1] etc. I'd like to return a single dataframe with the tidy tests stacked on top of each other. I really want to generalize this method for more combinations of predictors, so building a

Recoding Similar Factor Levels Across Multiple Data Frames Using Purrr and Dplyr

自作多情 提交于 2019-12-11 12:49:29
问题 Below are two simple data frames. I would like to re-code (collapse) the Sat1 and Sat2 columns so that all degrees of satisfied are coded simply as Satisfied , and all degrees of Dissatisfied are coded as Dissatisfied . Neutral will remain as Neutral. These factors will therefore have three levels - Satisfied, Dissatisfied, and Neutral . I would normally accomplish this by binding the data frames, and using lapply along with re-code from the car package, such as: DF1[2:3] <- lapply(DF1[2:3],