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
31
votes
7 answers

combine rows in data frame containing NA to make complete row

I know this is a duplicate Q but I can't seem to find the post again Using the following data df <- data.frame(A=c(1,1,2,2),B=c(NA,2,NA,4),C=c(3,NA,NA,5),D=c(NA,2,3,NA),E=c(5,NA,NA,4)) A B C D E 1 NA 3 NA 5 1 2 NA 2 NA 2 NA NA 3…
CPak
  • 13,260
  • 3
  • 30
  • 48
30
votes
1 answer

Duplicating (and modifying) discrete axis in ggplot2

I want to duplicate the left-side Y-axis on a ggplot2 plot onto the right side, and then change the tick labels for a discrete (categorical) axis. I've read the answer to this question, however as can be seen on the package's repo page, the…
Alex P. Miller
  • 2,128
  • 1
  • 23
  • 20
30
votes
3 answers

data.table equivalent of tidyr::complete()

tidyr::complete() adds rows to a data.frame for combinations of column values that are missing from the data. Example: library(dplyr) library(tidyr) df <- data.frame(person = c(1,2,2), observation_id = c(1,1,2), …
RoyalTS
  • 9,545
  • 12
  • 60
  • 101
28
votes
3 answers

R purrr:::pmap: how to refer to input arguments by name?

I am using R purrr:::pmap with three inputs. It is not clear how I can refer explicitly to these inputs in the formula call? When using map2, the formula call goes as ~ .x + .y. But how to do when using pmap? Reproducing Hadley's example from…
Matifou
  • 7,968
  • 3
  • 47
  • 52
27
votes
5 answers

tidyverse - prefered way to turn a named vector into a data.frame/tibble

Using the tidyverse a lot i often face the challenge of turning named vectors into a data.frame/tibble with the columns being the names of the vector. What is the prefered/tidyversey way of doing this? EDIT: This is related to: this and this…
Rentrop
  • 20,979
  • 10
  • 72
  • 100
26
votes
2 answers

Error in bind_rows_(x, .id) : Argument 1 must have names

Here is a code snippet: y <- purrr::map(1:2, ~ c(a=.x)) test1 <- dplyr::bind_rows(y) test2 <- do.call(dplyr::bind_rows, y) The first call to bind_rows (test1) generates the error Error in bind_rows_(x, .id) : Argument 1 must have names Using…
Robert McDonald
  • 1,250
  • 1
  • 12
  • 20
25
votes
10 answers

Canonical tidyverse method to update some values of a vector from a look-up table

I frequently need to recode some (not all!) values in a data frame column based off of a look-up table. I'm not satisfied by the ways I know of to solve the problem. I'd like to be able to do it in a clear, stable, and efficient way. Before I write…
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
24
votes
9 answers

How to name the list of the group_split output in dplyr

I have the following process which uses group_split of dplyr: library(tidyverse) set.seed(1) iris %>% sample_n(size = 5) %>% group_by(Species) %>% group_split() The result is: [[1]] # A tibble: 2 x 5 Sepal.Length Sepal.Width…
littleworth
  • 4,781
  • 6
  • 42
  • 76
24
votes
5 answers

R: convert to factor with order of levels same with case_when

When doing data analysis, I sometimes need to recode values to factors in order to carry out groups analysis. I want to keep the order of factor same as the order of conversion specified in case_when. In this case, the order should be "Excellent" …
user5068121
24
votes
3 answers

Having trouble viewing more than 10 rows in a tibble

First off - I am a beginner at programming and R, so excuse me if this is a silly question. I am having trouble viewing more than ten rows in a tibble that is generated from the following code. The code below is meant to find the most common words…
Meraj Shah
  • 243
  • 1
  • 2
  • 4
23
votes
1 answer

How do {{}} double curly brackets work in dplyr?

I saw Hadley's talk at RConf and he mentioned using double brackets for calling variables in tidy evals. I searched Google but I couldn't find anything talking about when to use them. What's the use case for double brackets in dplyr?
Cauder
  • 2,157
  • 4
  • 30
  • 69
23
votes
4 answers

pivot_longer into multiple columns

I am trying to use pivot_longer. However, I am not sure how to use names_sep or names_pattern to solve this. dat <- tribble( ~group, ~BP, ~HS, ~BB, ~lowerBP, ~upperBP, ~lowerHS, ~upperHS, ~lowerBB, ~upperBB, "1", 0.51, 0.15, 0.05, …
Droc
  • 257
  • 1
  • 2
  • 8
23
votes
1 answer

Order of operations in summarise

What is happening in the first line of code and why does the result differ from the two next results? library(tidyverse) library(magrittr) data.frame(A=c(2,2),B=c(1,1)) %>% summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B)) yields…
Martin
  • 331
  • 2
  • 10
23
votes
1 answer

round_any equivalent for dplyr?

I am trying to make a switch to the "new" tidyverse ecosystem and try to avoid loading the old packages from Wickham et al. I used to rely my coding previously. I found round_any function from plyr useful in many cases where I needed custom rounding…
Mikko
  • 7,530
  • 8
  • 55
  • 92
20
votes
4 answers

How to obtain a position of last non-zero element

I've got a binary variable representing if event happened or not: event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0) I need to obtain a variable that would indicate the time when the last event happened. The expected output would…
jakes
  • 1,964
  • 3
  • 18
  • 50
1
2 3
99 100