Questions tagged [tidyverse]

ONLY use this tag if your question relates to the installation, integration with your system, or inclusion of the entire tidyverse library. DO NOT USE if your question relates to one or two components of the tidyverse, such as dplyr or ggplot2. Use *those* tags, and tag with `r` as well for a better response.

tidyverse is an R package that installs a number of other packages for data processing and graphics.

Unless your question is about the entirety of the tidyverse package, its installation or its integration with your system, use tags for the packages you are actually using. Using library(tidyverse) is rarely a minimal reproducible example when only library(dplyr) is required.

See https://www.tidyverse.org/packages/ for a breakdown of the packages contained in tidyverse and their respective functions.

Repositories

Resources

Vignettes

Related tags

9739 questions
7
votes
2 answers

ggplot2 facet grid with conditional facets and tidy evaluation

I want to create a function that produces a ggplot plot and provide an optional argument for a facetting variable to facet_grid(). In particular, if possible, I want to incorporate the conditional logic inside facet_grid. I also want to use the…
user3646834
7
votes
4 answers

Collapse and merge overlapping time intervals

I am developing a tidyverse-based data workflow, and came across a situation where I have a data frame with lots of time intervals. Let's call the data frame my_time_intervals, and it can be reproduced like…
hpy
  • 1,989
  • 7
  • 26
  • 56
7
votes
1 answer

dplyr function with optional grouping only when argument provided

I need to write a dplyr function that creates a customised area plot. So here's my attempt. area_plot <- function(data, what, by){ by <- ensym(by) what <- ensym(what) data %>% filter(!is.na(!!by)) %>% group_by(date, !!by) %>% …
Kuba_
  • 886
  • 6
  • 22
7
votes
3 answers

Is there a equivalent for the tidyr fill() for strings in R?

So I have a data frame like this one: First Group Bob Joe John Jesse Second Group Jane Mary Emily Sarah Grace I would like to fill in the empty cells in the…
Sean
  • 145
  • 2
  • 12
7
votes
1 answer

tidyverse: row wise calculations by group

I am trying to do an inventory calculation in R which requires a row wise calculation for each Mat-Plant combination. Here's a test data set - df <- structure(list(Mat = c("A", "A", "A", "A", "A", "A", "B", "B" ), Plant = c("P1", "P1", "P1", "P2",…
Shree
  • 10,835
  • 1
  • 14
  • 36
7
votes
3 answers

Using any() vs | in dplyr::mutate

Why should I use | vs any() when I'm comparing columns in dplyr::mutate()? And why do they return different answers? For example: library(tidyverse) df <- data_frame(x = rep(c(T,F,T), 4), y = rep(c(T,F,T, F), 3), allF = F, allT = T) df %>% …
crazybilly
  • 2,992
  • 1
  • 16
  • 42
7
votes
3 answers

Purrr filter the nested data based on unnested variable containing character vectors

I have the data similar to df3. To reproduce the data, run the following: vec1 <- c("A", "B") vec2 <- c("A", "B", "C") df1 <- tibble::tribble( ~A, ~B, "X", 4L, "X", 9L, "Y", 5L, "Y", 2L, …
Geet
  • 2,515
  • 2
  • 19
  • 42
7
votes
1 answer

Mutate multiple variable to create multiple new variables

Let's say I have a tibble where I need to take multiple variables and mutate them into new multiple new variables. As an example, here is a simple tibble: tb <- tribble( ~x, ~y1, ~y2, ~y3, ~z, 1,2,4,6,2, 2,1,2,3,3, 3,6,4,2,1 ) I want to…
Jacob Nelson
  • 443
  • 1
  • 6
  • 16
7
votes
4 answers

iterate through data frame where each iteration is dependent on the previous item in R efficiently

I have a data frame with two vectors of length 5 and variable: x <- seq(1:5) y <- rep(0,5) df <- data.frame(x, y) z <- 10 I need to loop through the data frame and update y based on a condition related to x using z, and I need to update z at every…
George
  • 1,478
  • 17
  • 28
7
votes
1 answer

Replace NA in all columns of a dplyr chain

The question replace NA in a dplyr chain results into the solution dt %.% group_by(a) %.% mutate(b = ifelse(is.na(b), mean(b, na.rm = T), b)) with dplyr. I want to impute all colums with dplyr chain. There is no single column to group by, rather I…
hhh
  • 50,788
  • 62
  • 179
  • 282
7
votes
1 answer

How to fork/parallelize process in purrr::pmap

I have the following code that does serial processing with purr::pmap library(tidyverse) set.seed(1) params <- tribble( ~mean, ~sd, ~n, 5, 1, 1, 10, 5, 3, -3, 10, 5 ) params %>% pmap(rnorm) #> [[1]] #> [1] 4.373546 #> #>…
neversaint
  • 60,904
  • 137
  • 310
  • 477
7
votes
2 answers

Which is the best way to flatten nested lists derived from a relational database using tidyverse tools?

I have a nested list which have received from a REST call. The response includes a nested set of lists from an underlying relational database. I want to flatten the list to simplify analysis. I have tried to follow the guidelines in the purrr…
rgustavs
  • 97
  • 7
7
votes
1 answer

replace values throughout a tibble

In base R I can use the following code to remove/replace values throughout a dataframe (e.g., all NAs or values greater than 0.99). df[df > 0.99] <- NA df[is.na(df)] <- 0L Is there a way to perform the equivalent operation using dplyr in the…
Joe
  • 3,217
  • 3
  • 21
  • 37
7
votes
4 answers

dplyr: deselecting columns given by

How can I deselect columns given in the ... argument of a self written function. (I also need to select the columns at another point, so just specifying the columns with - in ... does not solve my problem.) Any soltions are apreciated,…
snaut
  • 2,261
  • 18
  • 37
7
votes
1 answer

Using group_by with mutate_if by column name

I am trying to use mutate_if to perform calculations based on the variable name. For example, if the variable names contains "demo" calculate the mean, and if the name contains "meas" calculate the…
Jake Thompson
  • 2,591
  • 1
  • 16
  • 32