100+ results for 'lang:r pivot_wider'

Not the results you expected?

signature_obj_operation.R (https://github.com/ShixiangWang/sigminer.git) R · 137 lines

106 dplyr::ungroup()

107

108 mat <- tidyr::pivot_wider(mat, names_from = "class", values_from = "signature") %>%

109 dplyr::select(-"base") %>%

110 tibble::column_to_rownames("context") %>%

analysis.R (https://github.com/jkaupp/tidytuesdays.git) R · 61 lines

29 order <- writer_percentages %>%

30 filter(binary == "PASS") %>%

31 #pivot_wider(names_from = binary, values_from = percentage, values_fill = 0) %>%

32 arrange(percentage) %>%

33 pull(writer)

BSU_plots.R (https://gitlab.com/pjbirrell/real-time-mcmc) R · 50 lines

28 create.subplot <- function(projections, data) {

29 plot <- projections %>%

30 pivot_wider(names_from = quantile) %>%

31 plot_ly(x = ~date, width = 800, height = plot.height) %>%

32 add_ribbons(ymin = ~`0.025`, ymax = ~`0.975`, color = I("lightblue2"), alpha = 0.25) %>%

pivot_wider.R (https://github.com/TysonStanley/tidyfast.git) R · 89 lines

2 #'

3 #'

4 #' \code{dt_pivot_wider()} "widens" data, increasing the number of columns and

5 #' decreasing the number of rows. The inverse transformation is

6 #' \code{dt_pivot_longer()}. Syntax based on the \code{tidyr} equivalents.

23 #' )

24 #'

25 #' dt_pivot_wider(example_dt, names_from = stuff, values_from = things)

26 #' dt_pivot_wider(example_dt, names_from = stuff, values_from = things, id_cols = z)

29 #'

30 #' @export

31 dt_pivot_wider <- function(dt_,

32 id_cols = NULL,

33 names_from,

34 names_sep = "_",

35 values_from) {

36 UseMethod("dt_pivot_wider", dt_)

37 }

38

functions.R (https://github.com/kjhealy/covdata.git) R · 216 lines

211 dplyr::group_by(year, cname) %>%

212 dplyr::arrange(year) %>%

213 tidyr::pivot_wider(names_from = year, values_from = n) %>%

214 dplyr::mutate(dplyr::across(where(is.character), dplyr::recode, .missing = "-")) %>%

215 dplyr::arrange(cname)

get_tableone.R (https://github.com/openpharma/visR.git) R · 129 lines

105 data_ns <- data %>%

106 dplyr::summarise(summary = dplyr::n()) %>%

107 tidyr::pivot_wider(names_from = tidyselect::any_of(strata), values_from = "summary") %>%

108 dplyr::mutate(variable = "Sample", summary_id = "N")

109

113 tidyr::pivot_longer(cols = setdiff(names(.), strata), names_to = "variable", values_to = "summary") %>%

114 tidyr::unnest_longer(summary) %>%

115 tidyr::pivot_wider(names_from = tidyselect::any_of(strata), values_from = "summary")

116

117 data_table1 <- rbind(data_ns, data_summary) %>%

ds_admin.ch.R (https://github.com/covid19datahub/COVID19.git) R · 123 lines

58 vaccinated <- vaccinated %>%

59 filter(age=="total_population") %>%

60 pivot_wider(id_cols = c("code", "date"), names_from = "type", values_from = "total") %>%

61 rename(people_vaccinated = COVID19AtLeastOneDosePersons,

62 people_fully_vaccinated = COVID19FullyVaccPersons)

test-embeddings.R (https://github.com/tidymodels/textrecipes.git) R · 352 lines

47 ) %>%

48 tidyr::unnest(token_num_binary) %>%

49 tidyr::pivot_wider(

50 names_from = dimension,

51 values_from = score

nzv.R (https://github.com/rstudio-conf-2020/applied-ml.git) R · 113 lines

48 group_by(name, value) %>%

49 count() %>%

50 tidyr::pivot_wider(names_from = value, values_from = n) %>%

51 rename(one = `1`, zero = `0`) %>%

52 filter(one < 20 | is.na(one)) %>%

kids-table.R (https://github.com/gkaramanis/tidytuesday.git) R · 104 lines

34 table_prepped <- libraries %>%

35 filter(year == 1997 | year == 2016) %>%

36 pivot_wider(id_cols = state, names_from = year, values_from = inf_adj_perchild) %>%

37 mutate(change = round((`2016` - `1997` ) / `1997` * 100)) %>%

38 inner_join(sparklines, by = "state") %>%

lmodel2-tidiers.R (https://github.com/tidymodels/broom.git) R · 113 lines

69 tidyr::separate(key, c("level", "term"), "-") %>%

70 mutate(level = ifelse(level == "2.5%", "conf.low", "conf.high")) %>%

71 tidyr::pivot_wider(c(Method, term),

72 names_from = level,

73 values_from = value

test-tidyr-pivot-wider.R (https://github.com/rstudio/sparklyr.git) R · 262 lines

7

8 sdf <- copy_to(sc, tibble::tibble(key = c("x", "y", "z"), val = 1:3))

9 pv <- tidyr::pivot_wider(

10 sdf, names_from = key, values_from = val, names_sort = TRUE

11 ) %>%

19

20 sdf <- copy_to(sc, tibble::tibble(a = 1, key = c("x", "y"), val = 1:2))

21 pv <- tidyr::pivot_wider(

22 sdf, names_from = key, values_from = val, names_sort = TRUE

23 ) %>%

31

32 sdf <- copy_to(sc, tibble::tibble(a = 1:2, key = c("x", "y"), val = 1:2))

33 pv <- tidyr::pivot_wider(

34 sdf, names_from = key, values_from = val, names_sort = TRUE

35 ) %>%

1_download_census_data.R (https://github.com/wxwx1993/PM_COVID.git) R · 146 lines

46 acs_data <- get_acs("county", variables = acs_vars, year = year)

47 acs_data$moe <- NULL

48 acs_data <- pivot_wider(acs_data,id_cols = c(GEOID, NAME), names_from = variable, values_from = estimate)

49 }

50

75 dec_data <- get_decennial("county", variables = dec_vars, year = year)

76 }

77 dec_data <- pivot_wider(dec_data, id_cols = c(GEOID, NAME), names_from = variable, values_from = value)

78 }

79

Reading.R (https://github.com/beeb22/makeovermonday.git) R · 68 lines

31

32 reading %>%

33 pivot_wider(names_from = category,

34 values_from = index_2015_100) %>%

35 clean_names() %>%

40 filter(date == as.Date("2015-04-01") | date == as.Date("2020-08-01")) %>%

41 select(country, real_books, date) %>%

42 pivot_wider(names_from = date, values_from = real_books) %>%

43 mutate(change = (`2020-08-01` - `2015-04-01`)*100) %>%

44 ggplot()+

show_group_enrichment.R (https://github.com/ShixiangWang/sigminer.git) R · 194 lines

115 if (isTRUE(cluster_row)) {

116 data2 <- data[, c(x, y, "grp_var", "measure_observed"), with = F]

117 data2 <- tidyr::pivot_wider(data2, names_from = x, values_from = "measure_observed")

118

119 get_cluster_order <- function(x) {

tidytuesday_201945_bike_and_walk_commutes.r (https://github.com/spren9er/tidytuesday.git) R · 93 lines

33 percentage = total / total_city_size

34 ) %>%

35 pivot_wider(names_from = mode, values_from = c(percentage, total)) %>%

36 select(

37 state, state_abb, city_size, percentage_city_size, percentage_walk,

kids.R (https://github.com/gkaramanis/tidytuesday.git) R · 56 lines

12 filter(year == 1997 | year == 2016) %>%

13 mutate(inf_adj_perchild = inf_adj_perchild * 1000) %>%

14 pivot_wider(id_cols = state, names_from = year, names_prefix = "year_", values_from = inf_adj_perchild) %>%

15 mutate(diff = year_2016 - year_1997) %>%

16 left_join(states_abbr)

model_eval.R (https://github.com/microsoft/forecasting.git) R · 50 lines

18 fcast <- fcast %>%

19 as_tibble() %>%

20 pivot_wider(

21 id_cols=all_of(c(keyvars, indexvar)),

22 names_from=.model,

tidytuesday_201932_bob_ross_paintings.r (https://github.com/spren9er/tidytuesday.git) R · 121 lines

53 filter(painted == 1) %>%

54 select(-painted) %>%

55 pivot_wider(names_from = color_name, values_from = total) %>%

56 replace(is.na(.), 0)

57

68

69 dist_mtx <- cut_residuals %>%

70 pivot_wider(object, names_from = color, values_from = cut_residual) %>%

71 column_to_rownames('object') %>%

72 as.matrix() %>%

mut_type_occurrences.R (https://github.com/UMCUGenetics/MutationalPatterns.git) R · 86 lines

75 dplyr::group_by(types, sample, .drop = FALSE) %>%

76 dplyr::summarise(count = dplyr::n(), .groups = "drop") %>%

77 tidyr::pivot_wider(names_from = types, values_from = count) %>%

78 dplyr::mutate(`C>T` = `C>T at CpG` + `C>T other`) %>%

79 dplyr::select(

show_sig_fit.R (https://github.com/ShixiangWang/sigminer.git) R · 86 lines

42 tibble::rownames_to_column("Sig") %>%

43 tidyr::pivot_longer(cols = -"Sig", names_to = "sample", values_to = "expo") %>%

44 tidyr::pivot_wider(id_cols = "sample", names_from = "Sig", values_from = "expo") %>%

45 data.table::as.data.table()

46 } else {

tidy.R (https://github.com/ikashnitsky/dataviz-mpidr.git) R · 96 lines

24

25 # to wide format

26 pop_w <- pivot_wider(data = pop, names_from = year, values_from = value)

27

28 # equivalently we can start using the piping operator ( %>% )

29 pop_w <- pop %>%

30 pivot_wider(names_from = year, values_from = value)

31

32 # back to long format

narrabri_rose_plants.r (https://github.com/APSIMInitiative/ApsimX.git) R · 74 lines

22 biomass <- read_xlsx(path = path, sheet = "Sheet1", range = "A1:J93")

23 biomass_longer <- biomass %>% pivot_longer(cols = 3:10, names_to = "treatment", values_to = "values")

24 biomass_wider <- biomass_longer %>% pivot_wider(names_from = Variate, values_from = values)

25 biomass_2009 <- biomass_wider %>% mutate(year = "2009") %>% select(year, DAS, everything())

26

32 biomass <- read_xlsx(path = path, sheet = "Sheet1", range = "A1:J73")

33 biomass_longer <- biomass %>% pivot_longer(cols = 3:10, names_to = "treatment", values_to = "values")

34 biomass_wider <- biomass_longer %>% pivot_wider(names_from = Variate, values_from = values)

35 biomass_2010 <- biomass_wider %>% mutate(year = "2010") %>% select(year, DAS, everything())

36

44 phenology <- bind_rows(firstsquare, firstflower) %>% select(-Variate) %>% select(year, variable, everything())

45 phenology_longer <- phenology %>% pivot_longer(c(3:10), names_to = "treatment", values_to = "values")

46 phenology_wider <- phenology_longer %>% pivot_wider(names_from = variable, values_from = values)

47 phenology_2009 <- phenology_wider

48

plot_doublingTime.R (https://github.com/chschoenenberger/covid19_dashboard.git) R · 56 lines

22 daysGrowthRate <- 7

23 data <- data_evolution %>%

24 pivot_wider(id_cols = c(`Province/State`, `Country/Region`, date, Lat, Long), names_from = var, values_from = value) %>%

25 filter(if (input$selectize_doublingTime_Variable == "doublingTimeConfirmed") (confirmed >= 100) else (deceased >= 10)) %>%

26 filter(if (is.null(input$selectize_doublingTime_Country)) TRUE else `Country/Region` %in% input$selectize_doublingTime_Country) %>%

tuber.R (https://github.com/soodoku/tuber.git) R · 129 lines

12 #' @importFrom tibble enframe

13 #' @importFrom tidyselect everything all_of

14 #' @importFrom tidyr pivot_wider unnest unnest_longer

15 #' @importFrom purrr map_df map_dbl

16 #' @docType package

carbon_footprint.R (https://github.com/rfordatascience/tidytuesday.git) R · 76 lines

53

54 tidy_table <- long_table %>%

55 tidyr::pivot_wider(names_from = Metric, values_from = Values) %>%

56 janitor::clean_names('snake')

57

get_popn_nhs.R (https://gitlab.com/pjbirrell/real-time-mcmc) R · 58 lines

19 pop <- pop %>%

20 filter(Sex == 4) %>%

21 pivot_wider(id_cols = NHSERApr19CD, names_from = Age, values_from = Population)

22 pop <- pop %>%

23 mutate("All ages" = apply(pop[, -1], 1, sum)) %>%

get_video_details.R (https://github.com/soodoku/tuber.git) R · 131 lines

12 intermediate <- res %>%

13 tibble::enframe() %>%

14 tidyr::pivot_wider() %>%

15 tidyr::unnest(cols = c(kind, etag)) %>%

16 # reflect level of nesting in column name

20_PredictorPorts.R (https://github.com/OpenSourceAP/CrossSection.git) R · 117 lines

56 filter(port == "LS") %>%

57 select(date, signalname, ret) %>%

58 pivot_wider(names_from = signalname, values_from = ret) %>%

59 arrange(date)

60

cochrane2df.R (https://github.com/massimoaria/bibliometrix.git) R · 63 lines

15 summarise(cont=paste(.data$content, collapse="---",sep="")) %>%

16 arrange(.data$Tag, .data$Paper) %>%

17 pivot_wider(names_from = .data$Tag,values_from = .data$cont) %>%

18 ungroup() %>%

19 rename("PY" = "YR",

preprocess-contact-rates.r (https://github.com/ImperialCollegeLondon/covid19model.git) R · 159 lines

81 for(period in c("weekend", "weekday")){

82

83 contact_tab_matrix.c.p <- pivot_wider(contact_tab[[Country]][[period]], id_cols= part.age, names_from = cont.age, values_from = m)

84 stopifnot( all( colnames(contact_tab_matrix.c.p)[-1] == contact_tab_matrix.c.p$part.age ) )

85 contact_tab_matrix[[Country]][[period]] <- unname(as.matrix(contact_tab_matrix.c.p[,-1]))

152

153 for(Country in countries){

154 contact_tab_matrix.c <- pivot_wider(contact_tab[[Country]], id_cols= part.age, names_from = cont.age, values_from = m)

155 stopifnot( all( colnames(contact_tab_matrix.c)[-1] == contact_tab_matrix.c$part.age ) )

156 contact_tab_matrix[[Country]] <- unname(as.matrix(contact_tab_matrix.c[,-1]))

tsibble2ts.R (https://github.com/tidyverts/tsibble.git) R · 216 lines

44 select(x, !!idx, !!!key_vars, !!value_var),

45 !!!vars_fill, .full = TRUE)

46 pivot_wider_ts(tsbl_sel, frequency = frequency)

47 }

48

49 pivot_wider_ts <- function(data, frequency = NULL) {

50 index <- index_var(data)

51 df_rows <- data[[index]]

plots_miscellaneous-data.R (https://github.com/J535D165/CoronaWatchNL.git) R · 140 lines

77 # summarise(Aantal = max(Aantal)) %>%

78 # ungroup() %>%

79 # pivot_wider(names_from = Type, values_from = Aantal) %>%

80 # mutate(

81 # `Positieve testen` = Positief - lag(Positief),

tidy-exercises-solutions.R (https://github.com/ikashnitsky/dataviz-mpidr.git) R · 107 lines

36

37 # - subset only the year 2004

38 # - transform to wide format using the column "sex" (pivot_wider)

39 # - get rid of the column for both sex

40 # - calculate the sex ratio (males to females)

43 pop %>%

44 filter(year == "2004") %>%

45 pivot_wider(names_from = sex, values_from = value) %>%

46 select(-b) %>%

47 mutate(sr = m / f) %>%

71 filter(age %in% 15:59, year == "2001") %>%

72 select(-year, -pop, -deaths) %>%

73 pivot_wider(names_from = sex, values_from = mx) %>%

74 select(-b) %>%

75 mutate(sr_mx = (m / f) %>% na_if(Inf)) %>% # replace Inf with NAs

tidyr-verbs.R (https://github.com/tidyverts/tsibble.git) R · 195 lines

19 }

20

21 pivot_wider.tbl_ts <- function(data, id_cols = NULL, names_from = name, ...) {

22 key_var <- vars_pull(names(data), !!enquo(names_from))

23 if (has_index(key_var, data)) {

30 new_key <- key_vars(remove_key(data, .vars = key_left))

31

32 tbl <- tidyr::pivot_wider(as_tibble(data),

33 id_cols = !!enquo(id_cols), names_from = !!enquo(names_from), ...)

34 tbl <- retain_tsibble(tbl, new_key, index(data))

tidytuesday_202032_european_energy.r (https://github.com/spren9er/tidytuesday.git) R · 169 lines

26 group_by(country, clean_electricity) %>%

27 summarize(total = sum(total)) %>%

28 pivot_wider(names_from = clean_electricity, values_from = total) %>%

29 mutate(ratio = `TRUE` / (`TRUE` + `FALSE`)) %>%

30 arrange(desc(ratio)) %>%

artefact_viewer.R (https://github.com/craddm/eegUtils.git) R · 169 lines

24 names_to = "electrode",

25 values_to = "value")

26 epoch_dat <- tidyr::pivot_wider(epoch_dat,

27 id_cols = c(epoch, electrode),

28 names_from = measure,

scrape_citydata.R (https://github.com/eeg31/police-reform.git) R · 132 lines

116 filter(str_detect(Function, "(Protection|Totals)")) %>%

117 filter(`Monthly_full-time_payroll` >= 0) %>%

118 pivot_wider(names_from = Function, values_from = `Monthly_full-time_payroll`) %>%

119 ggplot(aes(x=`Totals for Government`, y=`Police Protection - Officers`)) +

120 geom_point() +

test-verb-pivot-wider.R (https://github.com/hadley/dbplyr.git) R · 212 lines

10 expect_equal(

11 memdb_frame(key = c("x", "y", "z"), val = 1:3) %>%

12 tidyr::pivot_wider(names_from = key, values_from = val) %>%

13 collect(),

14 tibble(x = 1, y = 2, z = 3)

23 expect_snapshot(

24 lazy_frame(key = c("x", "y", "z"), val = 1:3) %>%

25 dbplyr_pivot_wider_spec(spec)

26 )

27 })

31

32 expect_equal(

33 dbplyr_pivot_wider_spec(df, spec) %>% op_vars(),

34 c("a", "x", "y")

35 )

test-tidyr.R (https://github.com/tidyverts/tsibble.git) R · 146 lines

18 ungroup()

19

20 test_that("pivot_wider()", {

21 out <- tsbl %>%

22 pivot_wider(names_from = group, values_from = value)

26 out_grp <- tsbl %>%

27 group_by(group) %>%

28 pivot_wider(names_from = group, values_from = value)

29 expect_equal(groups(out_grp), list())

30 out2 <- tourism %>%

31 pivot_wider(names_from = Purpose, values_from = Trips)

32 expect_equal(key_vars(out2), c("Region", "State"))

33 expect_equal(ncol(out2), 7)

broom-package.R (https://github.com/tidymodels/broom.git) R · 30 lines

16 #' @importFrom purrr map_df set_names possibly

17 #' @importFrom tibble tibble as_tibble

18 #' @importFrom tidyr pivot_longer pivot_wider

19 #' @importFrom dplyr select filter mutate mutate_at

20 #'

zzz.R (https://github.com/tidyverts/tsibble.git) R · 24 lines

4

5 s3_register("tidyr::pivot_longer", "tbl_ts")

6 s3_register("tidyr::pivot_wider", "tbl_ts")

7 s3_register("tidyr::gather", "tbl_ts")

8 s3_register("tidyr::spread", "tbl_ts")

taylor-swift-beyonce.R (https://github.com/rfordatascience/tidytuesday.git) R · 139 lines

21 separate(album_details, into = c("album_detail_type", "album_details"), sep = ": ") %>%

22 mutate(album_detail_type = if_else(album_detail_type == "Re-edition", "Re-release", album_detail_type)) %>%

23 pivot_wider(names_from = album_detail_type, values_from = album_details) %>%

24 select(-`na`) %>%

25 janitor::clean_names()

79 separate(album_details, into = c("album_detail_type", "album_details"), sep = ": ") %>%

80 mutate(album_detail_type = if_else(album_detail_type == "Re-edition", "Re-release", album_detail_type)) %>%

81 pivot_wider(names_from = album_detail_type, values_from = album_details) %>%

82 janitor::clean_names()

83

test_longer_wider.R (https://github.com/elbersb/tidylog.git) R · 117 lines

37 })

38

39 test_that("pivot_wider", {

40 expect_message({

41 outlog <- tidylog::pivot_wider(mtcars, names_from = vs, values_from = cyl)

45

46 expect_silent({

47 outtidyr <- tidyr::pivot_wider(mtcars, names_from = vs, values_from = cyl)

48 })

49

51 })

52

53 test_that("pivot_wider: argument order", {

54 expect_message({

55 out_ab <- tidylog::pivot_wider(names_from = vs, values_from = cyl, data = mtcars)

diverging_chart.R (https://github.com/thomas-neitmann/ggcharts.git) R · 212 lines

25 #' biomedicalrevenue %>%

26 #' dplyr::filter(year > 2016) %>%

27 #' tidyr::pivot_wider(

28 #' values_from = revenue,

29 #' names_from = year,

101 #' biomedicalrevenue %>%

102 #' dplyr::filter(year > 2016) %>%

103 #' tidyr::pivot_wider(

104 #' values_from = revenue,

105 #' names_from = year,

ff_column_totals.R (https://github.com/ewenharrison/finalfit.git) R · 203 lines

73 totals = totals %>%

74 dplyr::select(-c(grand_total, percent)) %>%

75 tidyr::pivot_wider(names_from = dependent, values_from = n) %>%

76 as.data.frame() %>%

77 dplyr::mutate(label = label,

enrich_component_strand_bias.R (https://github.com/ShixiangWang/sigminer.git) R · 101 lines

37 strand = ifelse(.data$strand == "T", "Trans", "UnTrans")

38 ) %>%

39 tidyr::pivot_wider(

40 id_cols = "component",

41 names_from = "strand",

yardstick-metric-sets.R (https://github.com/business-science/modeltime.git) R · 287 lines

162 dplyr::ungroup() %>%

163

164 tidyr::pivot_wider(

165 names_from = .metric,

166 values_from = .estimate

mod_jhu_death_rate.R (https://github.com/JohnCoene/coronavirus.git) R · 105 lines

57 dplyr::summarise(cases = sum(cases, na.rm = TRUE)) %>%

58 dplyr::ungroup() %>%

59 tidyr::pivot_wider(

60 id_cols = date,

61 names_from = type,

zzz.R (https://github.com/hadley/dbplyr.git) R · 61 lines

8 register_s3_method("dplyr", "filter", "tbl_lazy")

9

10 register_s3_method("tidyr", "pivot_wider", "tbl_lazy")

11 register_s3_method("tidyr", "pivot_longer", "tbl_lazy")

12 register_s3_method("tidyr", "fill", "tbl_lazy")

tidytuesday_201913_seattle_pets.r (https://github.com/spren9er/tidytuesday.git) R · 73 lines

18 filter(species %in% c('Cat', 'Dog'), !is.na(animals_name), year >= 2015) %>%

19 count(species, animals_name) %>%

20 pivot_wider(

21 names_from = species, values_from = n, values_fill = list(n = 0)

22 ) %>%

long_to_wide_converter.R (https://github.com/IndrajeetPatil/statsExpressions.git) R · 106 lines

100

101 # convert to wide?

102 if (spread && paired) data %<>% tidyr::pivot_wider(names_from = {{ x }}, values_from = {{ y }})

103

104 # final clean-up

01-individual-pieces.R (https://github.com/rstudio-education/datascience-box.git) R · 44 lines

25 ## put together in a tibble and add link to help keep track ----

26 tibble(headers, values) %>%

27 pivot_wider(names_from = headers, values_from = values) %>%

28 add_column(link = first_info_url)

29

pivot_longer.R (https://github.com/TysonStanley/tidyfast.git) R · 97 lines

2 #'

3 #'

4 #' \code{dt_pivot_wider()} "widens" data, increasing the number of columns and

5 #' decreasing the number of rows. The inverse transformation is

6 #' \code{dt_pivot_longer()}. Syntax based on the \code{tidyr} equivalents.

emerald_soilwater_2016.r (https://github.com/APSIMInitiative/ApsimX.git) R · 95 lines

83 # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.

84

85 sw2016 <- data %>% pivot_wider(names_from = layer_no, values_from = c(depth, sw, bd))

86

87 sw2016 <- sw2016 %>% arrange(year, sowing, date)

reexports.R (https://github.com/rstudio/sparklyr.git) R · 144 lines

7 #' @include tidyr_nest.R

8 #' @include tidyr_pivot_longer.R

9 #' @include tidyr_pivot_wider.R

10 #' @include tidyr_separate.R

11 #' @include tidyr_unite.R

119 #' Pivot wider

120 #'

121 #' See \code{\link[tidyr:pivot_wider]{pivot_wider}} for more details.

122 #'

123 #' @name pivot_wider

124 #' @export

125 #' @importFrom tidyr pivot_wider

126 NULL

127

fullTable.R (https://github.com/chschoenenberger/covid19_dashboard.git) R · 132 lines

3 data <- data_evolution %>%

4 filter(date == current_date) %>%

5 pivot_wider(names_from = var, values_from = c(value, value_new)) %>%

6 select(-date, -Lat, -Long) %>%

7 add_row(

equality-line.R (https://github.com/ikashnitsky/dataviz-mpidr.git) R · 74 lines

20 pivot_longer(3:50, names_to = "year") %>%

21 mutate(value = value / 100) %>%

22 pivot_wider(names_from = Sex, values_from = value) %>%

23 clean_names() %>%

24 separate(country_engl_name_nat_name, into = c("cntr", "rest"), sep = "/") %>%

test-pivot_wider.R (https://github.com/markfairbanks/tidytable.git) R · 181 lines

1 # tests from tidyr regarding pivot_wider

2

3 test_that("can pivot all cols to wide", {

4 df <- data.table(label = c("x", "y", "z"), val = 1:3)

5 pivot_df <- pivot_wider.(df, names_from = label, values_from = val)

6

7 expect_named(pivot_df, c("x", "y", "z"))

11 test_that("non-pivoted cols are preserved", {

12 df <- data.table(a = 1, label = c("x", "y"), val = 1:2)

13 pivot_df <- pivot_wider.(df, names_from = label, values_from = val)

14

15 expect_named(pivot_df, c("a", "x", "y"))

19 test_that("implicit missings turn into explicit missings", {

20 df <- data.table(a = 1:2, label = c("x", "y"), val = 1:2)

21 pivot_df <- pivot_wider.(df, names_from = label, values_from = val)

22

23 expect_equal(pivot_df$a, c(1, 2))

ds_github.cssegisanddata.covid19unified.R (https://github.com/covid19datahub/COVID19.git) R · 82 lines

61

62 # pivot

63 x <- tidyr::pivot_wider(x, id_cols = c("ID", "Date"), names_from = "Type", values_from = "Cases")

64

65 # map values

Format_signatures.R (https://github.com/UMCUGenetics/MutationalPatterns.git) R · 191 lines

43 signatures = signatures %>%

44 tidyr::pivot_longer(-sig, names_to = "Type_subtype", values_to = "values") %>%

45 tidyr::pivot_wider(names_from = sig, values_from = values) %>%

46 dplyr::mutate(Type = str_replace(Type_subtype, ".*\\[(.*)\\].*", "\\1"),

47 SubType = str_remove_all(Type_subtype, "\\[|\\]|\\>[A-Z]")) %>%

emerald_soilwater_2015.r (https://github.com/APSIMInitiative/ApsimX.git) R · 136 lines

96 # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.

97

98 sw2015_hill_furrow <- data %>% pivot_wider(id_cols = c(year, sowing, hill_furrow, date), names_from = layer_no, values_from = c(depth, sw, bd))

99

100 # long <- data %>% gather(key = "variable", value = "value", c(depth, sw, bd))

118 # turn (depth, sw, bd) rows for each layer number into columns so we can compare with simulation results.

119

120 #sw2015 <- data %>% pivot_wider(id_cols = c(year, sowing, date), names_from = layer_no, values_from = c(depth, sw, bd))

121 sw2015 <- data %>% pivot_wider(names_from = layer_no, values_from = c(depth, sw, bd))

127

128

129 help("pivot_wider")

130 help("pivot_longer")

131

genius_url.R (https://github.com/JosiahParry/geniusR.git) R · 136 lines

23 #' @export

24 #' @importFrom rvest session html_nodes html_node html_text

25 #' @importFrom tidyr pivot_wider fill separate replace_na

26 #' @importFrom stringr str_detect str_extract str_replace_all str_trim

27 #' @importFrom tibble tibble

94 str_detect(lyric, "\\[|\\]") ~ "meta",

95 TRUE ~ "lyric")) %>%

96 pivot_wider(names_from = type, values_from = lyric) %>%

97

98 #spread(key = type, value = lyric)

reexports.R (https://github.com/sparklyr/sparklyr.git) R · 174 lines

104 #' Pivot wider

105 #'

106 #' See \code{\link[tidyr:pivot_wider]{pivot_wider}} for more details.

107 #'

108 #' @name pivot_wider

109 #' @export

110 #' @importFrom tidyr pivot_wider

111 NULL

112

broadway-scrape.R (https://github.com/gkaramanis/tidytuesday.git) R · 29 lines

18 broadway_theatres <- tibble(type = broadway_type, value = broadway_value) %>%

19 mutate(n = rep(1:41, each = 4)) %>%

20 pivot_wider(names_from = type, values_from = value) %>%

21 select(theatre = name, address = address1, longitude, latitude) %>%

22 mutate(

pyramids.R (https://github.com/ikashnitsky/dataviz-mpidr.git) R · 168 lines

52 year == 2018, !sex == "T"

53 ) %>%

54 pivot_wider(names_from = sex, values_from = values) %>%

55 ggplot(aes(age))+

56 geom_step(aes(y = `F`), color = "purple")+

test-dt_pivot_wider.R (https://github.com/TysonStanley/tidyfast.git) R · 73 lines

1 # tests from tidyr regarding pivot_wider

2

3 test_that("can pivot all cols to wide", {

4 df <- data.table(label = c("x", "y", "z"), val = 1:3)

5 pivot_df <- dt_pivot_wider(df, names_from = label, values_from = val)

6

7 expect_named(pivot_df, c("x", "y", "z"))

12 test_that("non-pivoted cols are preserved", {

13 df <- data.table(a = 1, label = c("x", "y"), val = 1:2)

14 pivot_df <- dt_pivot_wider(df, names_from = label, values_from = val)

15

16 expect_named(pivot_df, c("a", "x", "y"))

20 test_that("implicit missings turn into explicit missings", {

21 df <- data.table(a = 1:2, label = c("x", "y"), val = 1:2)

22 pivot_df <- dt_pivot_wider(df, names_from = label, values_from = val)

23

24 expect_equal(pivot_df$a, c(1, 2))

select_helpers.R (https://github.com/TysonStanley/tidyfast.git) R · 54 lines

19 #' # example of using it with `dt_pivot_longer()`

20 #' df <- data.table(row = 1, var = c("x", "y"), a = 1:2, b = 3:4)

21 #' pv <- dt_pivot_wider(df,

22 #' names_from = var,

23 #' values_from = c(dt_starts_with("a"), dt_ends_with("b"))

rowData.R (https://github.com/waldronlab/curatedMetagenomicData.git) R · 61 lines

47 rowDataLong <-

48 purrr::map(taxized, ~ dplyr::select(.x, name, value)) |>

49 purrr::map_dfr(~ tidyr::pivot_wider(.x, values_from = "value")) |>

50 dplyr::select(tidyselect::all_of(rank_names)) |>

51 dplyr::mutate(across(.fns = ~ base::as.character(.x))) |>

55 rowDataNCBI <-

56 purrr::map(taxized, ~ dplyr::select(.x, name, id)) |>

57 purrr::map_dfr(~ tidyr::pivot_wider(.x, values_from = "id")) |>

58 dplyr::select(tidyselect::all_of(rank_names)) |>

59 dplyr::mutate(across(.fns = ~ base::as.integer(.x))) |>

narrabri_actuals.r (https://github.com/APSIMInitiative/ApsimX.git) R · 146 lines

78

79 plant_2006 <- bind_rows(lai_2006, sites_2006, bolls_2006)

80 plant_2006 <- plant_2006 %>% pivot_wider(names_from = variable, values_from = values)

81 plant_2006 <- plant_2006 %>% mutate(date = dmy("10/10/2006") + days(das))

82 plant_2006 <- plant_2006 %>% mutate(year = 2006) %>% select(year, Treatment, date, das, everything())

99

100 plant_2007 <- bind_rows(lai_2007, sites_2007, squares_2007, bolls_2007)

101 plant_2007 <- plant_2007 %>% pivot_wider(names_from = variable, values_from = values)

102 plant_2007 <- plant_2007 %>% mutate(date = dmy("15/10/2007") + days(das))

103 plant_2007 <- plant_2007 %>% mutate(year = 2007) %>% select(year, Treatment, date, das, everything())

126

127 plant_2008 <- bind_rows(lai_2008, sites_2008, squares_2008, bolls_2008, greenbolls_2008, openbolls_2008)

128 plant_2008 <- plant_2008 %>% pivot_wider(names_from = variable, values_from = values)

129 plant_2008 <- plant_2008 %>% mutate(date = dmy("15/10/2008") + days(das))

130 plant_2008 <- plant_2008 %>% mutate(year = 2008) %>% select(year, Treatment, date, das, everything())

0041-basic-word-stats-and-plots.R (https://github.com/benmarwick/saa-meeting-abstracts.git) R · 145 lines

51 group_by(word, year_group) %>%

52 summarise(mean_prop = mean(prop, na.rm = TRUE)) %>%

53 pivot_wider(names_from = year_group,

54 values_from = mean_prop) %>%

55 drop_na() %>%

126 df_tbl_prop_two_groups %>%

127 select(word, prop, year_group) %>%

128 pivot_wider(names_from = year_group,

129 values_from = prop,

130 values_fn = list(prop = mean))

02-functionalize.R (https://github.com/rstudio-education/datascience-box.git) R · 39 lines

24 # put together in a tibble and add link to help keep track ----

25 tibble(headers, values) %>%

26 pivot_wider(names_from = headers, values_from = values) %>%

27 add_column(link = x)

28

03-tidy-data.R (https://github.com/joanby/tidyverse-data-science.git) R · 148 lines

86

87 table2 %>%

88 pivot_wider(names_from = type, values_from = count)

89

90

ks-tidiers.R (https://github.com/tidymodels/broom.git) R · 70 lines

8 #'

9 #' @details Returns a data frame in long format with four columns. Use

10 #' \code{tidyr::pivot_wider(..., names_from = variable, values_from = value)}

11 #' on the output to return to a wide format.

12 #'

26 #'

27 #' td %>%

28 #' pivot_wider(c(obs, estimate),

29 #' names_from = variable,

30 #' values_from = value

build_nonUS_setup.R (https://github.com/HopkinsIDD/COVIDScenarioPipeline.git) R · 120 lines

92 # Make wide if specified

93 if(opt$w){

94 rc <- rc %>% tidyr::pivot_wider(OGEOID, names_from=DGEOID, values_from=FLOW, values_fill=c("FLOW"=0), values_fn = list(FLOW=sum))

95 }

96

test_generate_predictor.R (https://github.com/edunford/tidysynth.git) R · 236 lines

107 x3 = mean(x3)) %>%

108 tidyr::gather(variable,value,-unit) %>%

109 tidyr::pivot_wider(names_from = unit,values_from=value))

110

111

132 x3 = mean(x3)) %>%

133 tidyr::gather(variable,value,-unit) %>%

134 tidyr::pivot_wider(names_from = unit,values_from=value))

135 })

136

207 x3 = mean(x3)) %>%

208 tidyr::gather(variable,value,-unit) %>%

209 tidyr::pivot_wider(names_from = unit,values_from=value))

210

211

mod_table_world.R (https://github.com/JohnCoene/coronavirus.git) R · 66 lines

44 dplyr::summarise(cases = sum(cases)) %>%

45 dplyr::ungroup() %>%

46 tidyr::pivot_wider(country, names_from = type, values_from = cases) %>%

47 dplyr::arrange(-confirmed) %>%

48 dplyr::mutate(

longer_wider.R (https://github.com/elbersb/tidylog.git) R · 41 lines

7 pivot_wider <- function(data, ...) {

8 log_longer_wider(data, .fun = tidyr::pivot_wider, .funname = "pivot_wider", ...)

9 }

10

path_curves.r (git://github.com/ggobi/tourr.git) R · 40 lines

25 library(tidyr)

26 ggplot(

27 data = pivot_wider(df,

28 id_cols = c(obs, step),

29 names_from = var, names_prefix = "Var",

test-tidyr-pivot-wider.R (https://github.com/sparklyr/sparklyr.git) R · 270 lines

7

8 sdf <- copy_to(sc, tibble::tibble(key = c("x", "y", "z"), val = 1:3))

9 pv <- tidyr::pivot_wider(

10 sdf,

11 names_from = key, values_from = val, names_sort = TRUE

20

21 sdf <- copy_to(sc, tibble::tibble(a = 1, key = c("x", "y"), val = 1:2))

22 pv <- tidyr::pivot_wider(

23 sdf,

24 names_from = key, values_from = val, names_sort = TRUE

33

34 sdf <- copy_to(sc, tibble::tibble(a = 1:2, key = c("x", "y"), val = 1:2))

35 pv <- tidyr::pivot_wider(

36 sdf,

37 names_from = key, values_from = val, names_sort = TRUE

path-andrews.r (git://github.com/ggobi/tourr.git) R · 79 lines

29 #' library(tidyr)

30 #' ggplot(

31 #' data = pivot_wider(df,

32 #' id_cols = c(obs, step),

33 #' names_from = var, names_prefix = "Var",

test-rvar-.R (https://github.com/jgabry/posterior.git) R · 237 lines

141 d = rvar_from_array(x_array[4,, drop = FALSE])

142 )

143 expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref)

144 expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df)

145

152 d = c(rvar(NA), NA, NA, df$x[[4]]),

153 )

154 expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2)

155 })

156

test_synthetic_control.R (https://github.com/edunford/tidysynth.git) R · 192 lines

97 expected = controls %>%

98 dplyr::select(time_unit = time,outcome,unit) %>%

99 tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%

100 dplyr::filter(time_unit <= 1995))

101

107 dplyr::filter(unit!="e") %>%

108 dplyr::select(time_unit = time,outcome,unit) %>%

109 tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%

110 dplyr::filter(time_unit <= 1995))

111 })

177 expected = controls %>%

178 dplyr::select(time_unit = time,outcome,unit) %>%

179 tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%

180 dplyr::filter(time_unit <= 1995))

181 })

transform_seg_table.R (https://github.com/ShixiangWang/sigminer.git) R · 116 lines

2 #'

3 #' @inheritParams get_cn_freq_table

4 #' @inheritParams tidyr::pivot_wider

5 #' @param ref_type annotation data type used for constructing matrix.

6 #'

108 dplyr::select(-c("i.start", "i.end")) %>%

109 na.omit() %>%

110 tidyr::pivot_wider(

111 names_from = "sample", values_from = "segVal",

112 values_fill = values_fill, values_fn = values_fn

dog-friendliness.R (https://github.com/gkaramanis/tidytuesday.git) R · 70 lines

16 child_f <- dog_descriptions %>%

17 count(breed_primary, env_children) %>%

18 pivot_wider(names_from = env_children, names_prefix ="child_", values_from = n) %>%

19 mutate(

20 child_total = child_FALSE + child_TRUE,

24 cat_f <- dog_descriptions %>%

25 count(breed_primary, env_cats) %>%

26 pivot_wider(names_from = env_cats, names_prefix ="cat_", values_from = n) %>%

27 mutate(

28 cat_total = cat_FALSE + cat_TRUE,

utils_table.R (https://github.com/openpharma/visR.git) R · 140 lines

21 dplyr::summarise(N = dplyr::n()) %>%

22 dplyr::mutate(`%` = round(100 * N/sum(N), 3)) %>%

23 tidyr::pivot_wider(names_from = value, values_from = c("N", "%"), names_sep=" ") %>%

24 as.list()

25 list(dat)

90 dplyr::mutate(`n (%)` = paste0(N, " (", format(100 * N/sum(N), digits = 3, trim=TRUE), "%)")) %>%

91 dplyr::select(-N) %>%

92 tidyr::pivot_wider(names_from = value, values_from = c("n (%)"), names_sep=" ") %>%

93 as.list()

94 list(dat)

data-reshaping-lab-key.R (https://github.com/SISBID/Module1.git) R · 55 lines

52 ## -----------------------------------------------------------------------------

53 wide_by_route = long %>%

54 pivot_wider(names_from = route, values_from = n_people)

55

56

helper_normalize_by_feature.R (https://github.com/ShixiangWang/sigminer.git) R · 43 lines

35 tidyr::unnest(cols = c("context", "signature")) %>%

36 dplyr::ungroup() %>%

37 tidyr::pivot_wider(names_from = "class", values_from = "signature") %>%

38 dplyr::select(-"base") %>%

39 tibble::column_to_rownames(var = "context")

pivot_wider.R (https://github.com/markfairbanks/tidytable.git) R · 169 lines

32 #'

33 #' test_df %>%

34 #' pivot_wider.(names_from = b, values_from = vals)

35 #'

36 #' test_df %>%

37 #' pivot_wider.(

38 #' names_from = b, values_from = vals, names_prefix = "new_"

39 #' )

40 #' @export

41 pivot_wider. <- function(.df,

42 names_from = name,

43 values_from = value,

tidytuesday_201939_school_diversity.r (https://github.com/spren9er/tidytuesday.git) R · 60 lines

21 )

22 ) %>%

23 pivot_wider(names_from = school_year, values_from = c(diverse, total)) %>%

24 drop_na()

25

Data_Reshaping.R (https://github.com/SISBID/Module1.git) R · 116 lines

107

108 ## -----------------------------------------------------------------------------

109 wide = long %>% pivot_wider(names_from = "type",

110 values_from = "number")

111 wide

mod_table.R (https://github.com/JohnCoene/coronavirus.git) R · 56 lines

41 dplyr::filter(country == "China") %>%

42 dplyr::select(state, type, cases) %>%

43 tidyr::pivot_wider(state, names_from = type, values_from = cases) %>%

44 dplyr::arrange(-confirmed) %>%

45 dplyr::mutate(

pubmed2df.R (https://github.com/massimoaria/bibliometrix.git) R · 77 lines

19 summarise(cont=paste(.data$content, collapse="---",sep="")) %>%

20 arrange(.data$Tag, .data$Paper) %>%

21 pivot_wider(names_from = .data$Tag,values_from = .data$cont) %>%

22 ungroup() %>%

23 as.data.frame()

tidy-exercises.R (https://github.com/ikashnitsky/dataviz-mpidr.git) R · 56 lines

31

32 # - subset only the year 2004

33 # - transform to wide format using the column "sex" (pivot_wider)

34 # - get rid of the column for both sex

35 # - calculate the sex ratio (males to females)

51

52 # - subset only both sex

53 # - transform to wide format using the column "year" (pivot_wider)

54 # - calculate the growth of ASDR between 2005 and 2001

55 # Q: in which region the average growth/decrease in ASDR was largest?

Chicago_corr_lm.R (https://github.com/tidymodels/tune.git) R · 92 lines

66 summarize(res) %>%

67 select(threshold, .metric, mean) %>%

68 pivot_wider(names_from = .metric, values_from = mean, id_cols = threshold)

69

70 all_stats <- full_join(lm_stats, rs_stats)

plot_signature_strand_bias.R (https://github.com/UMCUGenetics/MutationalPatterns.git) R · 112 lines

63 dplyr::mutate(strand = ifelse(strand == "T", "transcribed", "untranscribed")) %>%

64 tidyr::pivot_longer(c(-strand, -type), names_to = "Signature") %>% # Combine signature columns

65 tidyr::pivot_wider(names_from = strand) %>% # Split transcribed/untranscribed column for binom test.

66 dplyr::mutate(

67 observed = as.integer(transcribed),

covmat.R (https://github.com/kingaa/pomp.git) R · 74 lines

23 summarize(value=(sd(value))) %>%

24 ungroup() %>%

25 pivot_wider(name) %>%

26 unlist() -> scale.dat

27

strand_bias_test.R (https://github.com/UMCUGenetics/MutationalPatterns.git) R · 82 lines

56 df_strand <- strand_occurrences %>%

57 dplyr::select(-relative_contribution) %>%

58 tidyr::pivot_wider(names_from = strand, values_from = no_mutations)

59

60 # Calculate total and ratio

friends-table.R (https://github.com/gkaramanis/tidytuesday.git) R · 72 lines

7 select(season, episode, imdb_rating) %>%

8 mutate(episode = paste0("E", str_pad(episode, 2, "left", "0"))) %>%

9 pivot_wider(names_from = episode, values_from = imdb_rating) %>%

10 mutate(season = paste0("S", str_pad(season, 2, "left", "0")))

11

test-pivot-wide.R (https://github.com/cran/tidyr.git) R · 217 lines

3 test_that("can pivot all cols to wide", {

4 df <- tibble(key = c("x", "y", "z"), val = 1:3)

5 pv <- pivot_wider(df, names_from = key, values_from = val)

6

7 expect_named(pv, c("x", "y", "z"))

11 test_that("non-pivoted cols are preserved", {

12 df <- tibble(a = 1, key = c("x", "y"), val = 1:2)

13 pv <- pivot_wider(df, names_from = key, values_from = val)

14

15 expect_named(pv, c("a", "x", "y"))

19 test_that("implicit missings turn into explicit missings", {

20 df <- tibble(a = 1:2, key = c("x", "y"), val = 1:2)

21 pv <- pivot_wider(df, names_from = key, values_from = val)

22

23 expect_equal(pv$a, c(1, 2))

nested-table.R (https://github.com/jthomasmock/reactable-presentation.git) R · 287 lines

22 count(year, month_name, complaint_type, animal_type) %>%

23 arrange(month_name, year) %>%

24 pivot_wider(names_from = month_name, values_from = n) %>%

25 filter(animal_type == "dog", between(year, 2014, 2019)) %>%

26 select(-animal_type) %>%

tidy-data.R (https://github.com/cran/tidyr.git) R · 128 lines

107 ## -----------------------------------------------------------------------------

108 weather3 %>%

109 pivot_wider(names_from = element, values_from = value)

110

111 ## -----------------------------------------------------------------------------

bench.R (https://github.com/cderv/dplyr.git) R · 240 lines

32 # %>%

33 # select(expression, version, median) %>%

34 # pivot_wider(names_from = version, values_from = median)

35 }

36