Answer by alistaire for dplyr syntax for arrow to sum columns specified in a...
I'm not sure why, but if you store the recursive code in a function (named or anonymous), it will let you run recursive code (or more simply written with Reduce):library(arrow)...
View ArticleAnswer by alistaire for Filter based on a list column using arrow and duckdb
This can be done with vectorized (evaluated rowwise) SQL functions, so you don't need rowwise(). If you look at the DuckDB list function docs you'd think "oh I could just write len(list_col &&...
View ArticleAnswer by alistaire for arrow::to_duckdb coerces int64 columns to doubles
If you look at ?to_duckdb, its con parameter defaults to arrow_duck_connection(), which if you look at it creates a DuckDB DBI connection withon <- DBI::dbConnect(duckdb::duckdb())If you look at...
View ArticleAnswer by alistaire for Arrow filter() with string expressions doesn't work...
You need to escape input with !! to tell filter() it's a value from the environment, not from the data: subset <- faithful_arrow |> filter(str_detect(size, str_to_title(!!input$size))) |>...
View ArticleAnswer by alistaire for dplyr left_join by less than, greater than condition
One option is to join row-wise as a list column, and then unnest the column:# evaluate each row individuallyfdata %>% rowwise() %>% # insert list column of single row of sdata based on conditions...
View ArticleAnswer by alistaire for Categorical variables of Int/Float types are lost...
If you explicitly go through a pyarrow.Table object, types are maintained:import pandas as pdimport pyarrow as paimport pyarrow.parquet as pqdf = pd.DataFrame({"a":['1','2','3']}).astype("category")tab...
View ArticleAnswer by alistaire for Join polygon only if they share a border
You can merge overlapping polygons with st_union(), though that does make a multipolygon, though you can cast that back to polygons. Your data has an issue, though: most of your polygons aren't quite...
View ArticleAnswer by alistaire for ggplot x-axis labels with all x values, multiple...
Two useful tools:scale_x_continuous(), like the other scale_*() functions, lets you specify a lot about how axes are structured and presented, including breaks, which determines where tick marks are....
View ArticleAnswer by alistaire for Is it possible to facet wrap across columns?
Reshape to long form (ggplot really wants data in long form) and it's much more straightforward:library(tidyverse)df <- data.frame( name = c("x", "y"), title1 = c(10L, 10L), title2 = c(10L, 10L),...
View ArticleAnswer by alistaire for Compare column by column in 2 data.frames with...
Matrix/array solutionThe way your data is structured is really as matrices/arrays, not data.frames, so here's a matrix solution:First make your data matrices and insert NAs for "None" strings:df1 <-...
View ArticleAnswer by alistaire for Counting by opening and closing dates
Here's a tidyverse solution using a few tools worth getting to know:library(tidyverse)# sample datadf <- tibble( town = c("A", "A", "A", "B"), opening = as.Date(c("1900-01-01", "1905-02-05",...
View ArticleAnswer by alistaire for Unnesting a list of lists in a data frame column
Note: Ignore the original and Update 1; Update 2 is better with the current state of the tidyverse.Original:With purrr, which is nice for lists,library(purrr)df %>% dmap(unlist)## # A tibble: 2 x...
View ArticleAnswer by alistaire for Calculate distance between multiple latitude and...
distm() returns a distance matrix, which is not what you want; you want the pairwise distances. So use the distance function (distHaversine(), distGeo(), or whatever)...
View ArticleAnswer by alistaire for How do I output from a (doubly) recursive function?
The function needs to [at least sometimes] return something interesting, and collect results as recursive calls return. Trying to keep the current logic the same, you want the roots from the first set...
View ArticleAnswer by alistaire for Rolling weighted sum across table with NA in R
I really quite like slider for sliding functions—it's very flexible, and has a purrr-like syntax. Here, slide_index_dbl() will let us slide a function and use another variable as an index by which to...
View ArticleAnswer by alistaire for Printing values from a list to a plot for each ID
You can pass geom_text() a label aesthetic, which will get separated by facet_wrap() nicely for you, e.g.library(tidyverse)set.seed(47L)df <- tibble( date = rep_len(seq(as.Date("2010-01-01"),...
View ArticleAnswer by alistaire for interoperability between Python and R
The docs weren't helping, so I went to the source, which led me to discover the internal py_resolve_envir() function which in the example in the question will return the R global environment, but won't...
View ArticleAnswer by alistaire for Plotting quantile regression by variables in a single...
While quantreg::plot.summary.rqs has an mfrow parameter, it uses it to override par('mfrow') so as to facet over parm values, which is not what you want to do.One alternative is to parse the objects...
View ArticleAnswer by alistaire for Plotting 3D Network
There may be a more elegant way to handle the adjacency matrix, but as far as I can tell rgl::segments3d() turns sequential points into segments, so you need to repeat points for each connection. The...
View ArticleAnswer by alistaire for How do I use variables for chunk options?
As documented hereeval.after is a package option, not a chunk option, so you need to set it with knitr::opts_knit$set(eval.after = ...). For example,---title: "eval.after"output: html_document---```{r...
View Article