R: gather multiple sets of columns with tidyr pivot_longer()

I am trying to gather two sets of columns. Each set needs to end up in a new column, but the rows data is linked, so sequential uses of tidyr::gather() won’t guarantee retention of cross-column organization. It looks like tidyr::pivot_longer() can accomplish this, but I am stumped. A similar post is here, but the answer… Read More R: gather multiple sets of columns with tidyr pivot_longer()

Is it possible to lengthen a given column based another column

This is what I want to achieve: For each child, I would like to have the mother right below him/her. library(tidyverse) mydata <- tribble( ~id, ~child, ~mother, 1, "John", "Jane", 1, "George", "Jane", 2, "Matthew", "Angela", 3, "Gwel", "Macrine", 3, "Tim", "Macrine", 3, "Jim", "Macrine" ) # Desired newdata <- tribble( ~id, ~child, ~mother, 1,… Read More Is it possible to lengthen a given column based another column

Pivot_longer from multiple columns pairwise

There is a dataset like this df <- data.frame( Pseudonym = c("aa", "bb"), KE_date_1 = c("2022-04-01", "2022-04-03"), KE_content_2 = c("high pot", "high pot"), KE_date_3 = c("2022-08-01", "2022-08-04"), KE_content_4 = c("high pot return", "high pot return") ) Pseudonym | KE_date_1 | KE_content_2| KE_date_3 | KE_content_4 ——————————————————————– aa | 2022-04-01| high pot | 2022-08-01 | high pot… Read More Pivot_longer from multiple columns pairwise

How to split elements separated by underscoe in columns names and create

I have the following dataset. As you could see, the columns names are composed by three elements. I would like to separate each of the element separted by the underscore: structure(list(post_name_mo = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526"), minna_yout_qu = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526"), tour_sima_ta = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526")), class = "data.frame", row.names = c(NA, -3L)) in a way… Read More How to split elements separated by underscoe in columns names and create

pivot_longer across multiple columns, how to use names_pattern parameter

df_wide <- structure(list(name = c("Crushers GC", "4Aces GC"), first_name = c("Charles", "Peter"), last_name = c("Howell III", "Uihlein"), first_name_1 = c("Paul", "Pat"), last_name_1 = c("Casey", "Perez"), first_name_2 = c("Bryson", "Dustin"), last_name_2 = c("DeChambeau", "Johnson"), first_name_3 = c("Anirban", "Patrick"), last_name_3 = c("Lahiri", "Reed")), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame")) name first_name last_name first_name_1 last_name_1… Read More pivot_longer across multiple columns, how to use names_pattern parameter

pivot_wider on multiple key:value pairs

I have a registration database, that looks like that reg <- tibble( name = c("John","Bill","Chuck"), email= c("john@john.com","bill@acme.com","chuck@someplace.org"), option1=c("meal","meal","drink"), value1=c("vegetarian","vegetarian","beer"), option2=c(NA,"drink","room"), value2=c(NA,"beer","single"), option3=c(NA,"room",NA), value3=c(NA,"single",NA) ) in other words, each of the three options (more in the real data, of course) can end up in different columns, and each registrant may have none, one or many options.… Read More pivot_wider on multiple key:value pairs