purrr

How to use map() with possibly()

核能气质少年 提交于 2021-02-20 09:42:31
问题 I am using map() to get post data from Facebook using the following code: posts_data <- map(posts$query_id, getPost, token = fb_oauth, n = 1000) However, some of the query_id observations are incorrect, or are shared events, which the API cannot retrieve and gives me an error like: Error in callAPI(url = url, token = token, api = api) : Unsupported get request. Object with ID '1816137521765810_1832190963493790' does not exist, cannot be loaded due to missing permissions, or does not support

lapply / purrr::map like function that allows access to the index by default?

帅比萌擦擦* 提交于 2021-02-20 04:29:08
问题 There's a workaround to allow access the index inside a s/lapply e.g. x <- list(a=11,b=12,c=13) lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x)) Is there any function like s/lapply (or like purrr::map() ) which allows access to the index in the simplest way possible, which I guess would be to simply supply its desired name to the initial function call and nothing more; map_with_index <- function(.x, .f, index) { # Same as purrr::map() # ..but whatever string

rename list of dataframe columns to mimic joined suffixes

青春壹個敷衍的年華 提交于 2021-02-19 07:18:07
问题 I have a list of dataframes: dd <- list() dd$data <- list( ONE = data.frame(inAll = c(1.1,1.2,1.3), inAll_2 = c(1.4,1.5,1.6)), TWO = data.frame(inAll = c(2.1,2.2,2.3), inAll_2 = c(2.4,2.5,2.6)), THREE = data.frame(inAll = c(3.1,3.2,3.3), inAll_2 = c(3.4,3.5,3.6)), FOUR = data.frame(inAll = c(4.1,4.2,4.3), inAll_2 = c(4.4,4.5,4.6)), FIVE = data.frame(inAll = c(5.1,5.2,5.3), inAll_2 = c(5.4,5.5,5.6)), SIX = data.frame(inAll = c(6.1,6.2,6.3), inAll_2 = c(6.4,6.5,6.6)) ) And then reduce those

R nested map through columns

前提是你 提交于 2021-02-19 06:18:08
问题 I got a function which was solved here. This function takes a column filled with annotations and another grouping column and propagates the annotation to rows with missing values. f1 <- function(data, group_col, expand_col){ data %>% dplyr::group_by({{group_col}}) %>% dplyr::mutate( {{expand_col}} := dplyr::case_when( !is.na({{expand_col}}) ~ {{expand_col}} , any( !is.na({{expand_col}}) ) & is.na({{expand_col}}) ~ paste(unique(unlist(str_split(na.omit({{expand_col}}), " ")) ), collapse = " ")

Error using 'segmented' with lm extracted from output of tidyverse 'map' in R

跟風遠走 提交于 2021-02-17 02:50:17
问题 I am using the 'segmented' package to find break points in linear regressions in R library(tidyverse) library(segmented) df <- data.frame(x = c(1:10), y = c(1,1,1,1,1,6:10)) lm_model <- lm(y ~ x, data = df) seg_model <- segmented(obj = lm_model, seg.Z = ~ x) But if I run the same model within a purrr:map, segmented fails. map_test <- df %>% nest() %>% mutate(map_lm = map(data, ~lm(y ~ x, data = .)), param_map_lm = map(map_lm, tidy)) map_lm_model <- map_test[[2]][[1]] map_seg_model <-

R: can I update the class of a an object in a magrittr pipe?

痞子三分冷 提交于 2021-02-11 15:54:02
问题 I have a piece of code where I update the class of the object. But I have to break the follow of the code to assign the class. Is there an elegant to to assign the class but continue the pipe so I have one pipe all the way to the final result? I suspect there might be something in {purrr}? library(disk.frame) library(dplyr) library(tidyquery) a = nycflights13::airports %>% as.disk.frame class(a) <- c(class(a), "data.frame") a %>% query("SELECT name, lat, lon ORDER BY lat DESC LIMIT 5") 回答1:

scraping with R using rvest and purrr, multiple pages

空扰寡人 提交于 2021-02-11 12:44:53
问题 I am trying to scrape a database containing information about previously sold houses in an area of Denmark. I want to retrieve information from not only page 1, but also 2, 3, 4 etc. I am new to R but from an tutorial i ended up with this. library(purrr) library(rvest) urlbase <- "https://www.boliga.dk/solgt/alle_boliger-4000ipostnr=4000&so=1&p=%d" map_df(1:5,function(i){ cat(".") page <- read_html(sprintf(urlbase,i)) data.frame(Address = html_text(html_nodes(page,".d-md-table-cell a")))

Rendering multiple parametrized Rmarkdown files by render_function() fails

痴心易碎 提交于 2021-02-11 12:41:44
问题 I wrote a parametrized report that will create individual reporting for ~120 centers in two different languages. The reports are created by the knit-button so far (which works perfectly fine). Inspired by @djnavarro's tweet (https://twitter.com/djnavarro/status/1101623527872970754) I tried writing a function to render multiple parametrized reports in one command, however, the function fails with Error as shown in the minimal reprex below: The .Rmd-file called summary_cyl.Rmd --- output: pdf

Rendering multiple parametrized Rmarkdown files by render_function() fails

只谈情不闲聊 提交于 2021-02-11 12:40:56
问题 I wrote a parametrized report that will create individual reporting for ~120 centers in two different languages. The reports are created by the knit-button so far (which works perfectly fine). Inspired by @djnavarro's tweet (https://twitter.com/djnavarro/status/1101623527872970754) I tried writing a function to render multiple parametrized reports in one command, however, the function fails with Error as shown in the minimal reprex below: The .Rmd-file called summary_cyl.Rmd --- output: pdf

Long vectors stringdist package R

旧时模样 提交于 2021-02-11 05:59:27
问题 I posted a question some days ago and while the solution seems to be working on RStudio in Windows (but takes forever and sometimes spits out no results), I keep getting an error of long vectors not supported when I run the same code with 30 CPUs on a HPC. Any ideas why? Here is a sample of the data: > head(forfuzzy) # A tibble: 6 x 3 grantee_name grantee_city grantee_state <chr> <chr> <chr> 1 (ICS)2 MAINE CHAPTER CLEARWATER FL 2 (SUFFOLK COUNTY) VANDERBILT~ CENTERPORT NY 3 1 VOICE TREKKING A