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
8
votes
5 answers

Using str_extract in R to extract a number before a substring with regex

I would like to use str_extract in the stringr package to extract the numbers from strings in the form XX nights etcetc. I'm currently doing this: library(stringr) str_extract("17 nights$5 Days", "(\\d)+ nights") but that returns "17…
Harry M
  • 1,848
  • 3
  • 21
  • 37
8
votes
6 answers

Find variable combinations that makes Primary Key in R

Here is my toy dataframe. df <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, ~var6, ~var7, "A", "C", 1L, 5L, "AA", "AB", 1L, "A", "C", 2L, 5L, "BB", "AC", 2L, "A", "D", 1L, 7L, "AA", "BC", …
Geet
  • 2,515
  • 2
  • 19
  • 42
8
votes
5 answers

Carry / use value from previous group

data: structure(list(id = c(1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5), ax = c("a", "a", "b", "b", "b", "b", "b", "b", "c", "c", "d", "d", "e"), time = c(1, 3, 0, 2, 4, 5, 6, 8, 7, 9, 10, …
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
8
votes
2 answers

dplyr mutate using variable columns

I am trying to use mutate to create a new column with values based on a specific column. Example final data frame (I am trying to create new_col): x = tibble(colA = c(11, 12, 13), colB = c(91, 92, 93), col_to_use = c("colA",…
burger
  • 5,683
  • 9
  • 40
  • 63
8
votes
1 answer

Using tidy eval for multiple dplyr filter conditions

I'm new to tidy eval and trying to write generic functions- one thing I'm struggling with right now is writing multiple filter conditions for categorical variables. This is what I'm using right now- create_expr <- function(name, val){ …
Mridul Garg
  • 477
  • 1
  • 8
  • 17
8
votes
5 answers

How to call a function for each row of a data.frame?

I have a function with several paramters. This function returns a data.frame. I have another data.frame. Now I would like to call my function for each row of my data.frame (as parameters). The resulting data.frames I would like to rbind. So I…
JerryWho
  • 3,060
  • 6
  • 27
  • 49
8
votes
1 answer

r group lag sum

I have some data with groups for which I want to compute a summary (sum or mean) over a fixed number of periods. I'm trying to do this with a group_by followed by mutate and then operating with the variable and its dplyr::lag. Here is an…
josemz
  • 1,283
  • 7
  • 15
8
votes
2 answers

Extract longest word in string

I would like to find and extract the longest word of a string, if possible using a tidyverse package. library(tidyverse) tbl <- tibble(a=c("ab cde", "bcde f", "cde fg"), b=c("cde", "bcde", "cde")) tbl # A tibble: 3 x 1 a 1 ab cde 2 bcde…
moabit21
  • 639
  • 8
  • 20
8
votes
1 answer

What is causing tidyr warning message: "attributes are not identical across measure variables"

I have a tibble, df: > df # A tibble: 4 x 5 profile Sepal.Length Sepal.Width Petal.Length Petal.Width 1 Profile 1 -1.011 0.850 -1.301 -1.251 2 Profile 2 …
Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
8
votes
2 answers

How do pipes work with purrr map() function and the "." (dot) symbol

When using both pipes and the map() function from purrr, I am confused about how data and variables are passed along. For instance, this code works as I expect: library(tidyverse) cars %>% select_if(is.numeric) %>% map(~hist(.)) Yet, when I…
D.Hadley
  • 1,179
  • 13
  • 13
8
votes
1 answer

Naming a new variable based on a quosure

I'm trying to write a custom function that will compute a new variable based on values from a predefined vector of variables (e.g., vector_heavy) and then name the new variable based on an argument provided to the function (e.g., custom_name). This…
Joe
  • 3,217
  • 3
  • 21
  • 37
8
votes
2 answers

How to detect an empty quosure in rlang?

f <- function(x) enquo(x) e <- f() # #~ None of these work: > is_empty(e) [1] FALSE > is_missing(e) [1] FALSE > is_false(e) [1] FALSE > is_quosure(e) [1] TRUE
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
8
votes
1 answer

Replacing subsets in R with Tidyverse

How do one replace the values of a subset in R with Tidyverse? Using the cars data as an example, if I would like to change all the speed lower than 30 into 0, I can use the command below: cars[cars["speed"] < 30,] <- 0 With Tidyverse, one can…
Carl H
  • 1,036
  • 2
  • 15
  • 27
8
votes
2 answers

defining custom dplyr methods in R package

I have a package with custom summary(), print() methods for objects that have a particular class. This package also uses the wonderful dplyr package for data manipulation - and I expect my users to write scripts that use both my package and…
Andrew
  • 9,090
  • 8
  • 46
  • 59
7
votes
3 answers

Merge two dataframes based on similar time interval in R

I have two data frames like the dummy samples , df1 (main data set) and df2. These data originally coming from two different log data. df1 is always complete dataset from 02:00 midnight until 02:00 next day with different stream values during this…
DanG
  • 689
  • 1
  • 16
  • 39