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
16
votes
3 answers

Implicit sorting in tidyr::spread and dplyr::summarise

My data are ordered observations and I want to keep the ordering as much as possible while doing manipulations. Take the answer for this question, I put "B" ahead of "A" in the dataframe. The resulting wide data are sorted by the column "name",…
Dong
  • 481
  • 4
  • 15
15
votes
2 answers

Complete dataframe with missing combinations of values

I have a data frame with two factors (distance) and years (years). I would like to complete all years values for every factor by 0. i.e. from this: distance years area 1 NPR 3 10 2 NPR 4 20 3 NPR 7 30 4 100 …
maycca
  • 3,848
  • 5
  • 36
  • 67
15
votes
2 answers

How to Transpose (t) in the Tidyverse Using Tidyr

Using the sample data (bottom), I want to use the code below to group and summarise the data. After this, I want to transpose, but I'm stuck on how to use tidyr to achieve this? For context, I'm attempting to recreate an existing table that was…
Mike
  • 2,017
  • 6
  • 26
  • 53
15
votes
1 answer

How to spread columns with duplicate identifiers?

A have the following tibble: structure(list(age = c("21", "17", "32", "29", "15"), gender = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), row.names = c(NA, -5L), class = c("tbl_df",…
Dambo
  • 3,318
  • 5
  • 30
  • 79
15
votes
3 answers

R: fill down multiple columns

I'm using fill() from the tidyr package. fill(df, colname1, colname2, colname3) works fine, until I found a dataset with 32 variables. How should I fill down all columns without typing each name? I've tried: fill(df,colnames(df)), fill(df,1:32),…
woshishui
  • 1,906
  • 2
  • 15
  • 22
15
votes
6 answers

Reshaping data in R with "login" "logout" times

I'm new to R, and am working on a side project for my own purposes. I have this data (reproducable dput of this is at the end of the question): X datetime user state 1 1 2016-02-19 19:13:26 User1 joined 2 2 2016-02-19…
Tim Coker
  • 6,484
  • 2
  • 31
  • 62
15
votes
2 answers

Separating column using separate (tidyr) via dplyr on a first encountered digit

I'm trying to separate a rather messy column into two columns containing period and description. My data resembles the extract below: set.seed(1) dta <- data.frame(indicator=c("someindicator2001", "someindicator2011", …
Konrad
  • 17,740
  • 16
  • 106
  • 167
15
votes
2 answers

What is the dplyr equivalent of plyr::ldply(tapply) in R?

Ultimately, I am trying to achieve something similar to the following, but leveraging dplyr instead of plyr: library(dplyr) probs = seq(0, 1, 0.1) plyr::ldply(tapply(mtcars$mpg, mtcars$cyl, function(x) {…
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
14
votes
4 answers

Error installing tidyr on Ubuntu 18.04 & R 4.0.2

In trying to install the package tidyverse, I get errors in the installation of dependency tidyr. Here is the tail of the message I get: cpp11.cpp:31:100: error: ‘unmove’ is not a member of ‘cpp11’ return…
msoftrain
  • 1,017
  • 8
  • 23
14
votes
1 answer

R: Changing column names in pivot_wider() -- suffix to prefix

I'm trying to figure out how to alter the way in which tidyr's pivot_wider() function creates new variable names in resulting wide data sets. Specifically, I would like the "names_from" variable to be added to the prefix of the new variables rather…
mkpcr
  • 431
  • 1
  • 3
  • 13
14
votes
3 answers

parallel pivot_longer of two sets of columns

I have the following data frame: library(tidyverse) dat <- tribble( ~Scenario, ~V1, ~V2, ~V3, ~V4, 1, 0.97, 0.46, 0.79, 0.25, 1, 0.21, 0.45, 0.23, 0.63, 1, 0.95, 0.97, 0.07, 0.61, 1, 0.93, 0.79,…
deschen
  • 10,012
  • 3
  • 27
  • 50
14
votes
1 answer

Separate a column into 2 columns at the last underscore in R

I have a dataframe like this id <-c("1","2","3") col <- c("CHB_len_SCM_max","CHB_brf_SCM_min","CHB_PROC_S_SV_mean") df <- data.frame(id,col) I want to create 2 columns by separating the "col" into the measurement and stat. stat is basically the…
Sharath
  • 2,225
  • 3
  • 24
  • 37
14
votes
3 answers

using tidyverse; counting after and before change in value, within groups, generating new variables for each unique shift

I am looking for a tidyverse-solution that can count occurrences of unique values of TF within groups, id in the data datatbl. When TF changes I want to count both forward and backwards from that point. This counting should be stored in a new…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
14
votes
3 answers

tidyr use separate_rows over multiple columns

I have a data.frame where some cells contain strings of comma separate values: d <- data.frame(a=c(1:3), b=c("name1, name2, name3", "name4", "name5, name6"), c=c("name7","name8, name9", "name10" )) I want to separate those strings…
user23413
  • 308
  • 2
  • 7
14
votes
3 answers

Reshape from long to wide and create columns with binary value

I am aware of the spread function in the tidyr package but this is something I am unable to achieve. I have a data.frame with 2 columns as defined below. I need to transpose the column Subject into binary columns with 1 and 0. Below is the data…
sachinv
  • 492
  • 2
  • 5
  • 18