purrr

purrr: joining tibbles nested in different list columns

你说的曾经没有我的故事 提交于 2019-12-24 07:18:31
问题 This is essentially a follow-up question to a previous one which @keqiang-li kindly answered. I have a dataframe which includes a list column (nested data-frames) comprising parties to a government and the number of their respective seats. This dataframe is split per country (note I used the new dplyr 0.8 group_nest and group_split ). What I am essentially trying is to get another list column which features for each goverment a list for each previous government which holds a dataframe

Add column to df that's the output of a function that uses different column values combined to be a vector input

a 夏天 提交于 2019-12-24 06:35:50
问题 This is a very simplified version of my actual problem. My real df has many columns and I need to perform this action using a select from a character vector of column names. library(tidyverse) df <- data.frame(a1 = c(1:5), b1 = c(3,1,3,4,6), c1 = c(10:14), a2 = c(9:13), b2 = c(3:7), c2 = c(15:19)) df a1 b1 c1 a2 b2 c2 1 1 3 10 9 3 15 2 2 1 11 10 4 16 3 3 3 12 11 5 17 4 4 4 13 12 6 18 5 5 6 14 13 7 19 Let's say I wanted to get the cor for each row for selected columns using mutate - I tried:

unquote a list of functions inside R dplyr functions

纵然是瞬间 提交于 2019-12-24 03:23:51
问题 I was trying to pass a list of functions into dplyr summerize_at function and got a warning: library(tidyverse) library(purrr) p <- c(0.2, 0.5, 0.8) p_names <- map_chr(p, ~paste0(.x*100, "%")) p_funs <- map(p, ~partial(quantile, probs = .x, na.rm = TRUE)) %>% set_names(nm = p_names) mtcars %>% group_by(cyl) %>% summarize_at(vars(mpg), funs(!!!p_funs)) #> Warning: funs() is soft deprecated as of dplyr 0.8.0 #> please use list() instead #> #> # Before: #> funs(name = f(.) #> #> # After: #> list

In R, write each nested data frame to a CSV

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:25:34
问题 PROBLEM: I have data frame of data frames from purrr and want to write each nested data frame to a CSV. > df # A tibble: 3 × 2 dataset data <chr> <list> 1 aab <tibble [681 × 60]> 2 aae <tibble [1,486 × 173]> 3 acm <tibble [3,496 × 139]> That is, I want 3 CSVs from above: one CSV for each tibble under "data". I prefer tidyverse functions to lapply or similar. POTENTIAL SOLUTION I think it's gotta be something using map() or similar function: df %>% map(~write_csv(data, file=[how to get

concatenate periods to get time sequences, simultaneously for different starting points

帅比萌擦擦* 提交于 2019-12-24 01:09:28
问题 I have the following example data: library(data.table) set.seed(42) t <- data.table(time=1:1000, period=round(runif(100,1,5))) p <- data.table(id=1:10, cut=sample(1:100,5)) > t[62:71] time period 1: 62 5 2: 63 4 3: 64 3 4: 65 4 5: 66 2 6: 67 2 7: 68 4 8: 69 4 9: 70 2 10: 71 1 > head(p) id cut 1: 1 63 2: 2 22 3: 3 99 4: 4 38 5: 5 91 6: 6 63 where t gives some vector of periods associated with time points, and p gives for each person a cutoff in time . For each person in p , I would like to

Write multiple data frames to csv-file using purrr::map [duplicate]

与世无争的帅哥 提交于 2019-12-23 16:25:07
问题 This question already has answers here : Writing multiple data frames into .csv files using R (2 answers) Closed 2 years ago . PROBLEM: I have a list of dataframes which should be written to disk as csv-files. Assume this is the list of data frames: dfs <- list(iris, mtcars) WHAT DID NOT WORK: I have tried to build the correct file names like this, but it did not work: dfs %>% map(~paste0("data-raw/", ., ".csv")) I hoped that this bit would correctly give back the file names as strings.

How can I use accumulate like reduce2 function in purrr?

馋奶兔 提交于 2019-12-23 15:44:50
问题 I would like to use the accumulate function with two input vectors and the reduce2 function. The documentation for accumulate implies that two input vectors can be given and that accumulate can work with reduce2 . However, I am having trouble. Here is an example, inspired by the documentation from reduce2 . This is the example from reduce2 > paste2 <- function(x, y, sep = ".") paste(x, y, sep = sep) > letters[1:4] %>% reduce2(.y=c("-", ".", "-"), paste2) [1] "a-b.c-d" Here are several

Sampling different numbers of rows by group in dplyr tidyverse

▼魔方 西西 提交于 2019-12-23 12:53:33
问题 I'd like to sample rows from a data frame by group. But here's the catch, I'd like to sample a different number of records based on data from another table. Here is my reproducible data: df <- data_frame( Stratum = rep(c("High","Medium","Low"), 10), id = c(1:30), Value = runif(30) ) sampleGuide <- data_frame( Stratum = c("High","Medium","Low"), Surveys = c(3,2,5) ) Output should look like this: # A tibble: 10 × 2 Stratum Value <chr> <dbl> 1 High 0.21504972 2 High 0.71069005 3 High 0.09286843

Converting data frame into deeply nested list

六月ゝ 毕业季﹏ 提交于 2019-12-23 09:59:17
问题 I'm trying to create a data structure that the whisker package expects, and I can't seem to figure out how create that structure from my data frame. Let's say I have the following data frame: library(dplyr) existing_format <- mtcars %>% select(carb, gear, cyl) %>% arrange(carb, gear, cyl) %>% distinct() ...I would like to go from existing_format to the following desired format (only first two elements of desired_format list are shown): desired_format <- list( list( carb = "1", gear = list(

Extracting to a data frame from a JSON generated multi-level list with occasional missing elements

主宰稳场 提交于 2019-12-23 06:04:30
问题 I'm pulling soccer data through an API - the resulting JSON is returned as a list; dput example below: list(list(id = 10332894L, league_id = 8L, season_id = 12962L, aggregate_id = NULL, venue_id = 201L, localteam_id = 51L, visitorteam_id = 27L, weather_report = list(code = "drizzle", temperature = list(temp = 53.92, unit = "fahrenheit"), clouds = "90%", humidity = "87%", wind = list(speed = "12.75 m/s", degree = 200L)), attendance = 25098L, leg = "1/1", deleted = FALSE, referee = list(data =