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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
11 views

2 dots in odds ratio plot in R

I tried to plot the OR values and I get 2 dots. Please help. My code is- model11<- multinom(`Infected` ~ `Gender`, data = Gpop) summary(model11) tidy(model11, conf.int = TRUE) tbl_regression(...
Katy Baker's user avatar
3 votes
3 answers
47 views

Write all strings beginning with a certain substring in a specific range of columns to a new column in R with dplyr

title, the issue is simple and one i’ve been able to solve with nested loops but i understand those are not preferred in R programming, is there a tidyr way to accomplish this? i want to iterate over ...
Jsters's user avatar
  • 33
0 votes
1 answer
38 views

how to group by time (quarter) and another group

I have data that I've downloaded from a server. It has product sales per month and the products have various categories. I would like to summarise the data by category and by quarter. I can't figure ...
user20453870's user avatar
0 votes
2 answers
37 views

Aggregate the columns we create with pivot_wider (set a difference)

In my dataset i have individuals with sometimes 2 timepoints, and i would like to set the difference between the 2 timepoints. The thing is that i have a lot of columns which i would like to apply ...
BPeif's user avatar
  • 192
1 vote
3 answers
53 views

How can I convert the format of my dataframe into a new transposed format in R?

I would like to convert my dataframe, df1, to the format shown in df2. How can I do this in an efficient manner without hard-coding & while using R libraries? df1 <- data.frame( A = c(1, 5, 3,...
piper180's user avatar
  • 351
0 votes
0 answers
69 views

How to calculate cumulative proportion of population with the occurrence of an event over time?

I want to calculate the cumulative proportion of patients with the occurrence of an event based on a simulation. I was expecting the output to look something like instead I am generating the plot ...
Ravua1992's user avatar
1 vote
1 answer
37 views

Splitting columns with different number of elements into rows

I'm working with a bioinformatics tool called OrthoFinder, which puts genes from different genomes/species into what are called Orthogroups based on sequences similarity. One of the output files tells ...
Nanka's user avatar
  • 11
0 votes
1 answer
28 views

How to make longer based on ID column, *without* collapsing the remaining columns? - R

Based on this question and answer, that is close to satisfying my needs: R - how to make long rows by matching an element of original rows df <- data.table::data.table(ID = c(123,123,123,234,456, ...
Sky Scraper's user avatar
1 vote
4 answers
95 views

Separate multiple columns of nested data and clean them at the same time using base R and Tidyverse

Question: Is there a way for me to separate multiple nested columns and clean them all at the same time using base R and tidyverse? I've checked the other nested responses here, but nothing seems to ...
RoninUTA's user avatar
0 votes
1 answer
45 views

How would you create a recursively nested tibble in R

I trying to create what I'm calling a recursively nested tibble, a tibble with x rows that is nested such that each x row nests the following n rows of y. Here is an example of what I'm trying to ...
Mutuelinvestor's user avatar
0 votes
2 answers
43 views

pivot longer by looping over certain columns

I have the following data which belongs to my darts league in wide format structure(list(home_team = c("Art of Dart", "Art of Dart", "Art of Dart", "Art of Dart&...
Emre Toros's user avatar
1 vote
1 answer
72 views

Unnest only one column of nested data frames

Given a nested data frame like for example library(tidyr) library(dplyr) m <- mtcars %>% nest_by(cyl) I want to extract one of the columns in the nested data frames to the top level, creating ...
akraf's user avatar
  • 3,062
0 votes
1 answer
64 views

Pivot_wider with two value columns

I have the following dataset: filtered_input. It has the columns: year_group, exporter, FVA_origin, FYavgShares. year_group has four different values. The set is pre-sorted by descending value of ...
Melike's user avatar
  • 17
-4 votes
1 answer
75 views

Scraping Amazon review issue in R

I am trying to scrape a Amazon product page, more specifically the review section. I have tried different codes, one of them is this: if(!"pacman" %in% installed.packages()[,"Package&...
Anisa's user avatar
  • 49
0 votes
3 answers
42 views

How to split a variable (within a dataset) in R that is cut at different points (based on the location of the characters)

I am currently working on a project where I have to split a variable into two parts. I have already looked at similar problems, but they have not helped me. The original data set was created with SPSS....
sunny's user avatar
  • 3
0 votes
1 answer
43 views

How to use fill function from tidyr R package with groups

This question is a follow up to my other question, How to get tidy fill function to use groups when filling data rows. I want to get one row per TIMESTAMP value. I want to fill in real data, by ...
user8229029's user avatar
1 vote
1 answer
16 views

pivot_longer three variables respecting uniquely identifier rows

I have the following dataset. I want to pivot_longer() so I get three variables: (1) nutrition_site corresponding the longer version of the variables nutrition_site1:nutrition_site8; (2) gps_lat ...
Paula's user avatar
  • 605
2 votes
3 answers
54 views

Is there a function to get the column names generated by `pivot_wider`?

I am using pivot_wider with (potentially) multiple names_from and values_from columns. The resulting automatically generated column names are fine for my purposes. My question is, is there a ...
TY Lim's user avatar
  • 573
1 vote
2 answers
74 views

convert data from wide to long using pivot_longer()

I need your help reshaping data from wide to long, converting multiple cols/variables into rows. Below is the data. stack1 <- tibble::tribble( ~ID, ~Recall1, ~Recall2, ~Recall3, ~Recall4, ~...
azizi tamimi's user avatar
0 votes
2 answers
49 views

Grouping values of a column into categories in R

I have data in R that I want to compare by using the values in the first column. The category name is already included in the first column before the actual values of that category. The issue is that, ...
melisa's user avatar
  • 103
0 votes
2 answers
62 views

What alternative is faster to create new rows based on an external vector in R?

A typical use case where we want to duplicate rows to fit an external vector of length > 1 is when we want to introduce new dates, or allow for each date to show a different individual. Imagine ...
Alberto Agudo Dominguez's user avatar
1 vote
1 answer
39 views

Using summarise in wide vs long format

I have a dataframe with the format below. Status has two levels (PRE,POST). SI_mean TU_mean ED_mean MT_mean DT_mean SK_mean ATT_mean Status 2.6 2.75 2.6 2.8 3.4 2.5 3.8 PRE 3 3 2.4 2.4 3 3 4 PRE 2....
Derf's user avatar
  • 497
0 votes
1 answer
52 views

[tidy]: package 'tidy' is not available

I'm getting this error The following package(s) were not installed successfully: - [tidy]: package 'tidy' is not available You may need to manually download and install these packages. while running ...
Yasin Amini's user avatar
1 vote
1 answer
46 views

Error with replace_na single column. `replace` must be a list, not a number

I want to replace all NA in one column with 0. Here is my MWE: df = structure(list(stage = c("CKD12", "CKD12", "CKD12", "CKD12", "CKD3a", "CKD3a&...
Mark Davies's user avatar
4 votes
2 answers
88 views

How would you hoist latitude and longitude from a dataset in one stroke

I have been playing with nested lists lately and being able to extract data from deep levels. I run into a little problem with tidyr function hoist(). I am able to pull latitude and longitude for 5 ...
f0nzie's user avatar
  • 1,156
0 votes
2 answers
61 views

manipulate data from nested df and lists

I want to study some stocks or financial indices. I ise the package yfR and the yf_get function to download data from Yahoo Finance. This function returns a df with numerous variables. I want to ...
Hans Larsen's user avatar
1 vote
1 answer
52 views

How to include an argument from another function into a new function

I am trying to wrap a function from tidyr into my new function but one argument is being ignored. See example below: library(tidyr) one2many2 <- function(data, var) { data <- data %>% ...
Salvador's user avatar
  • 1,385
1 vote
1 answer
20 views

Referencing a function argument as a column name in pivot_longer

I'm trying to write a function that uses pivot_longer, and would like to use my function object as the object of the names_to argument in pivot_longer. record <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ...
Catherine's user avatar
4 votes
1 answer
54 views

nest to get list-columns of vectors rather than list-column of data frames

tidyr::nest() creates a list-column of data frames/tibbles: library(tidyverse) iris %>% nest(data = c(-Species)) #> # A tibble: 3 × 2 #> Species data #> <fct> ...
lost's user avatar
  • 1,553
2 votes
5 answers
91 views

Spliting column by multiple delimiters (keeping them) and into unequal number of columns

lets say I have a dataframe like this (simplified, similar version of my problem) : ID <- c(1,2,3) value <- c("1+4-3", "2+7-6+4-3", "-1+3") df <- data.frame(ID, ...
Honza88's user avatar
  • 92
0 votes
1 answer
22 views

How to reorganize table by group and/or add a new heading on an current heading?

I have a raw df with 4 columns: "Year", "Variable", "Frquency" and "%". The years go from 2011 to 2022; and the variable correspond to an age, with ...
Julia_MOura's user avatar
4 votes
1 answer
56 views

Pivot_longer() dataset by splitting the header's character strings into multiple columns

My data set contains values for multiple scenarios (S1, S2, S3) across multiple years, for a linking variable called link_col. link_col <- c("A0001", "A0002", "A0003") ...
Rlearn's user avatar
  • 79
0 votes
1 answer
58 views

Keep 1 row of a group if any rows of the group meet a condition

As part of a larger data-set, I have data along the lines of the following: data <- structure( list( ID = c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4), Event = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,...
sirius's user avatar
  • 5
0 votes
1 answer
106 views

Parsing FORMAT and sample value fields from VCF file

I am trying to load a vcf file (created using GATK using data.table::fread, and then parse the results of the FORMAT and sample fields, into different columns. The column names are given by the FORMAT ...
milcs40's user avatar
0 votes
1 answer
49 views

Removing duplicates based on 3-4 columns (dplyr)

I realize this might have been asked before but I am struggling to correctly remove duplicates in my df. I have used the method recommended here, but it's not removing all duplicates. #install ...
maldini1990's user avatar
1 vote
3 answers
68 views

unnest() when nulls are stored as empty lists

I would like to unnest() a data frame where NULL values are stored as list(). How can I do it? library(dplyr) library(tidyr) df <- tibble::tribble( ~date, ~id, '2023-07-21', ...
joshbrows's user avatar
  • 436
7 votes
4 answers
163 views

Pivot longer in R [duplicate]

Here is my input dataset which contains one row and 12 columns/variables: df <- data.frame(q_q10=0,q_q20=0, q_q30=0,q_q40=400, q_q10_low=0,q_q20_low=0,q_q30_low=0,q_q40_low=350, ...
Chloe_pa's user avatar
  • 183
0 votes
1 answer
61 views

Randomly draw values from an empirical distribution by group for multiple columns

This question is a follow up question to this one. The following code creates a tibble that includes the empirical distributions of the iris dataset columns Sepal.Length, Sepal.Width, Petal.Length and ...
MatSchu's user avatar
  • 417
1 vote
1 answer
56 views

convert dataframe from horizontal to vertical

I have data frame with values in this shape: col x1 y1 x2 y2 x3 y3 red 23 0.5 25 0.7 30 0.8 blue 12 08 45 0.1 31 0.5 I need to convert this into two columns ...
user22178063's user avatar
0 votes
1 answer
60 views

Map a list and extract elements to create a data frame in R

This is similar to the question I posted here. I use the nhlapi package and the nhl_schedule_seasons function to obtain a list of games and their associated info for a specific season. Using this for ...
Michael's user avatar
  • 271
0 votes
1 answer
89 views

Rearrange and align columns based on matching values in data frame using R

I have a data frame in R, and I want to rearrange the columns based on matching values within each column. However, in many cases, the columns don't match in such plaec in empty row. Here's an example ...
Umar's user avatar
  • 391
0 votes
1 answer
34 views

Tidyr/Dplyr: Iterative counts until a specific value is found. "How many records were added before value X was entered?"

I have a dataset of call logs that I need to do an iterative count until a specific value is found. The idea is answering the question "How many records were entered until this specific value is ...
Checht's user avatar
  • 57
0 votes
1 answer
71 views

Is there an R function to select specific row numbers?

I'm a beginner in the process of learning R and I have a dataset containing book names and authors that I'm using as practice for cleaning data. In part of the process, I want to seperate the column &...
Caitlyn Manfre's user avatar
1 vote
2 answers
78 views

melt equivalent of pivot_longer with multiple name patterns

I'm currently translating an app in data.table syntax and came across a pivot_longer, where the column names get seperated into two columns based on a pattern: library(data.table) library(tidyr) dt &...
Ludwig's user avatar
  • 611
2 votes
2 answers
56 views

cbind within a conditional ifelse statement

I have several data frames that are generated by a script, and sometimes they are empty. At the end of my procedure I would like to use cbind to put all of the data frames together, however this of ...
tlmoore's user avatar
  • 189
4 votes
1 answer
68 views

Sum columns based on a string match in other columns

I have a data fame with a structure similar to this: df <- data.frame("id1"=c("A", "A", NA, "B", "A", "A"), "val1&...
tlmoore's user avatar
  • 189
0 votes
2 answers
56 views

using sep argumnt to put double rounded parenthesis

I would like to close in double parenthesis the second column I am trying to unite library(tidyr) mtcars %>% summarise(mean = mean(mpg), sd = sd(mpg)) %>% tidyr::unite('A', 1:...
12666727b9's user avatar
  • 1,015
0 votes
2 answers
102 views

Separate Value in R (Table)

I have one table consisting of location value (long lat) such as below: How to code in R for separating this long and lat value I have used func separate but return to error (it is chr datatype). But ...
Pramadia Adhie Lazuardi's user avatar
0 votes
1 answer
31 views

Make wide dataframe long with different variable names

I have a dataframe that looks like this: example <- data.frame( date = as.Date(c('2001-01-01', '2001-01-02', '2001-01-01', '2001-01-02')), ...
jo_'s user avatar
  • 687
0 votes
0 answers
42 views

Randomly sample dataframe (slice_sample) and calculate summary statistics after using group_by, and iterate 999 times?

I would like to resample my dataframe (data_long) and calculate summary statistics (mean and standard deviation) of a numeric response variable (experience), after grouping data based on two ...
Benjamin Jones's user avatar

1
2 3 4 5
86