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
1
vote
1 answer

Collapsing dummy columns in R

I have a tibble in which each row corresponds to a person. There are multiple rows per person, but each row contains the exact same data for each person, EXCEPT for the final several columns (below, "won", "lost") which contain 1/0 dummy variables.…
Anna Jones
  • 91
  • 4
1
vote
1 answer

How to pivot wide to long format for columns with different names?

I have a wide df that I would like to convert to long form based on multiple columns. For example, all columns with "Type" would be in a single column and all columns with "Finding" would be in a single column. I believe dplyr is the best approach…
T1802173
  • 101
  • 4
1
vote
1 answer

R pivot_wider Error: `times` can't be missing. Location 1 is missing

I receive the error Error: times can't be missing. Location 1 is missing. In addition: Warning message: In nrow * ncol : NAs produced by integer overflow when calling: data_indicator <- data_pivoted %>% select(ID, var) %>% mutate(val = TRUE) %>%…
Chris
  • 156
  • 1
  • 1
  • 9
1
vote
1 answer

Calculation for last month -- with conditions

In my dataset, I have observations on different people, for each month, in different places. I want to create a new column which is: The average of value for last month for all places the person visited other than the place of the current row. So…
asd
  • 131
  • 4
1
vote
3 answers

Reshape wide to long, multiple columns and multiple time periods

I'm trying to reshape data from wide to long and I cant get the answer. The structure is as follows: id n_dep_1 n_dep_2 bleed_1 bleed_2 secretion_1 secretion_2 eva_1 eva_2 1 1 2 1 1 1 1 9 7 2 2 1 0 1 1 0 5 6 3 2 1 1 0 1 1 7 5 note: i…
1
vote
1 answer

How can I make new observations from columns in R?

I received a dataset from a survey, and it has a rather strange structure. Every row has 90 variables, with the first 15 being general questions and the other 75 being 5 sets of 15 variables, that are actually observations. The concrete case is an…
1
vote
1 answer

expand all columns using column names

I would like to get all possible combinations of all column values in a dataframe e.g. library(tidyr) # example data fruits <- tibble( type = c("apple", "orange", "banana"), year = c(2010, 2011, 2012), size = c("XS", "S", "M"), …
flee
  • 1,253
  • 3
  • 17
  • 34
1
vote
2 answers

Pivot numeric value column wider based on logical columns

I have a table that has a signal value and additional columns defining the type of signal. Each location has all types of signal. See example: d <-…
gdeniz
  • 169
  • 9
1
vote
1 answer

Retaining all columns in `tidyr::pivot_wider()` output

I am trying to convert a data in long format to wide format using tidyr::pivot_wider() but running into problems. Data Let's say this is my example dataset library(dplyr) library(tidyr) (dataEx <- structure( list( random1 = c(10, 10, 10, 10,…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
1
vote
2 answers

Creating multiple proportion table in R dataframe

I've the following table Result_Group Review A 1 B 4 A 1 C 1 D 5 D 4 E 5 C 1 C 2 A 2 B 3 E 2 df = structure(list(Result_Group = structure(c(1L, 2L, 1L, 3L, 4L, 4L, 5L, 3L, 3L, 1L, 2L, 5L), .Label = c("A", "B", "C",…
Luther_Proton
  • 348
  • 1
  • 7
1
vote
1 answer

Creating new variable by focusing range of other variables

I want to create a variable that include all numbers between (startyear) and (endyear - 1). My data looks like this: country leader startyear endyear US Eisenhower 1953 1961 US Kennedy 1961 1963 I want to show my data like…
Yusuf
  • 29
  • 4
1
vote
1 answer

Suggestion/Guidance on methods for unnesting a dataframe when the level of nesting is unknown and changes from row to row?

I'm working on a project using data from Etsy's API. Specifically, I am looking at Etsy's category taxonomy. Each row in the dataframe represents one category node, with a nested data frame containing all of its child nodes. I am not sure how many…
1
vote
2 answers

Split one column into two, retain original value if there aren't two values

I have a column with specific gene locations in my data frame. It either has the single location (in case of a SNP) or it has a range for the locations (in case of Deletion, Duplication). I want to split the Gene_Locations column into two columns:…
Ar1229
  • 131
  • 2
  • 9
1
vote
2 answers

tidyr::extract with Regex

I have the following data frame: df <- data.frame( Name= c('AMLOD VALSAR HCT MPH Filmtabl 10+160+25mg 100Stk','ARTHROTEC 50 Bitabs 50+0.2mg 50Stk','GLUCOPHAGE Filmtabl 850mg 100Stk'), Aug20Cu= c(1000,1831,7430), Sep20Cu= c(899,822, 1000) ) …
user15983252
  • 31
  • 1
  • 2
1
vote
2 answers

WIDE to LONG Dataframe with pivot_longer and names_pattern with Regular Expressions

I have a dataframe of repetaed measures in WIDE format, with an ID variable (pid) some non time-varyant variables (i.e. age and sex) and mostly time-varyant variables, all named similarly with the following scheme: spec_var_0 spec_var_1 and…
Diego
  • 127
  • 2
  • 13