Questions tagged [tidyr]

tidyr is an R package by Hadley Wickham for cleaning and reshaping data, designed to use the magrittr pipe (%>%) so as to interact well with dplyr and similar pipeable packages which emphasize tidy data. tidyr is the successor to reshape2.

tidyr is an package developed by Hadley Wickham and many others for cleaning and reshaping data, designed to use the pipe (%>%) so as to interact well with and similar pipeable packages which emphasize tidy data. tidyr is the successor to .

Links:

4200 questions
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
30
votes
4 answers

Using spread with duplicate identifiers for rows

I have a long form dataframe that have multiple entries for same date and person. jj <- data.frame(month=rep(1:3,4), student=rep(c("Amy", "Bob"), each=6), A=c(9, 7, 6, 8, 6, 9, 3, 2, 1, 5, 6, 5), B=c(6, 7, 8,…
Polar Bear
  • 731
  • 1
  • 7
  • 21
28
votes
0 answers

Comparison between dplyr::do / purrr::map, what advantages?

When using broom I was used to combine dplyr::group_by and dplyr::do to perform actions on grouped data thanks to @drob. For example, fitting a linear model to cars depending on their gear…
aurelien
  • 859
  • 1
  • 15
  • 22
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
24
votes
3 answers

pivot_longer with multiple classes causes error ("No common type")

I am running pivot_longer on multiple columns (i.e. two character columns and one numeric). I am encountering an error related to the class mismatch. I have investigated the documentation for any "force" options and did not see any arguments within…
Ryan Baxter-King
  • 339
  • 1
  • 2
  • 11
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
3 answers

Unnesting a list of lists in a data frame column

To unnest a data frame I can use: df <- data_frame( x = 1, y = list(a = 1, b = 2) ) tidyr::unnest(df) But how can I unnest a list inside of a list inside of a data frame column? df <- data_frame( x = 1, y = list(list(a = 1, b =…
emehex
  • 9,874
  • 10
  • 54
  • 100
22
votes
1 answer

Add NAs to make all list elements equal length

I'm doing a series of things in dplyr, tidyr, so would like to keep with a piped solution if possible. I have a list with uneven numbers of elements in each component: lolz <- list(a = c(2,4,5,2,3), b = c(3,3,2), c=c(1,1,2,4,5,3,3), d=c(1,2,3,1),…
jalapic
  • 13,792
  • 8
  • 57
  • 87
20
votes
3 answers

splitting multiple values in one column into multiple rows R

I have a data frame which for the most part is one observation per row. However, some rows have multiple values: # A tibble: 3 x 2 `number` abilities 1 51 b1261 2 57 …
pluke
  • 3,832
  • 5
  • 45
  • 68
20
votes
5 answers

How to control new variables' names after tidyr's spread?

I have a dataframe with panel structure: 2 observations for each unit from two years: library(tidyr) mydf <- data.frame( id = rep(1:3, rep(2,3)), year = rep(c(2012, 2013), 3), value = runif(6) ) mydf # id year value #1 1 2012…
janosdivenyi
  • 3,136
  • 2
  • 24
  • 36
20
votes
2 answers

Reshape multiple values at once

I have a long data set I would like to make wide and I'm curious if there is a way to do this all in one step using the reshape2 or tidyr packages in R. The data frame df looks like this: id type transactions amount 20 income 20 …
Dirk Calloway
  • 2,569
  • 4
  • 23
  • 34
18
votes
2 answers

Transposing data frames

Happy Weekends. I've been trying to replicate the results from this blog post in R. I am looking for a method of transposing the data without using t, preferably using tidyr or reshape. In example below, metadata is obtained by transposing…
ExperimenteR
  • 4,453
  • 1
  • 15
  • 19
17
votes
5 answers

Spreading a two column data frame with tidyr

I have a data frame that looks like this: a b 1 x 8 2 x 6 3 y 3 4 y 4 5 z 5 6 z 6 and I want to turn it into this: x y z 1 8 3 5 2 6 4 6 But calling library(tidyr) df <- data.frame( a = c("x", "x", "y", "y", "z", "z"), b = c(8, 6, 3,…
ljos
  • 315
  • 3
  • 9
17
votes
4 answers

Long to wide data with tidyR?

I have data that looks something like this df = data.frame(name=c("A","A","B","B"), group=c("g1","g2","g1","g2"), V1=c(10,40,20,30), V2=c(6,3,1,7)) I want to reshape it to look like this: df =…
Ignacio
  • 7,646
  • 16
  • 60
  • 113
17
votes
3 answers

R re-arrange dataframe: some rows to columns

I'm not even sure how to title the question properly! Suppose I have a dataframe d: Current dataframe: d <- data.frame(sample = LETTERS[1:2], cat = letters[11:20], count = c(1:10)) sample cat count 1 A k 1 2 B l 2 3 …
crs
  • 323
  • 1
  • 2
  • 8