tidyverse

Mutate multiple variable to create multiple new variables

Deadly 提交于 2019-11-26 11:37:31
问题 Let\'s say I have a tibble where I need to take multiple variables and mutate them into new multiple new variables. As an example, here is a simple tibble: tb <- tribble( ~x, ~y1, ~y2, ~y3, ~z, 1,2,4,6,2, 2,1,2,3,3, 3,6,4,2,1 ) I want to subtract variable z from every variable with a name starting with \"y\", and mutate the results as new variables of tb. Also, suppose I don\'t know how many \"y\" variables I have. I want the solution to fit nicely within tidyverse / dplyr workflow. In

Spread with duplicate identifiers (using tidyverse and %>%) [duplicate]

ぐ巨炮叔叔 提交于 2019-11-26 11:18:27
问题 This question already has answers here : Reshaping data in R with “login” “logout” times (6 answers) Closed 2 years ago . My data looks like this: I am trying to make it look like this: I would like to do this in tidyverse using %>%-chaining. df <- structure(list(id = c(2L, 2L, 4L, 5L, 5L, 5L, 5L), start_end = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L), .Label = c(\"end\", \"start\"), class = \"factor\"), date = structure(c(6L, 7L, 3L, 8L, 9L, 10L, 11L), .Label = c(\"1979-01-03\", \"1979-06-21\"