0

My current dataframe is of the format:

     name               GRADUATION_RATE                      varname count
  Example Name 1                  13.0                    Bus Stops   251
  Example Name 1                  13.0            Childcare Centers    87
  Example Name 2                  14.0                    Bus Stops   300
  Example Name 2                  14.0            Childcare Centers    60

I want to expand the dataframe such that it creates new columns based on the variable name's count, so there would be as many new columns as the number of unique varnames. So the expected output for the above would be:

     name               GRADUATION_RATE     Bus Stops_count Childcare centers_count 
  Example Name 1                  13.0                  251                      87
  Example Name 2                  14.0                  300                      60

I'm guessing this is like the opposite of a dplyr melt, but I'm not sure how to do it.

Phil
  • 7,287
  • 3
  • 36
  • 66
Aryan poonacha
  • 451
  • 2
  • 4
  • 15
  • `tidyr::pivot_wider(mydf, names_from = varname, values_from = count)` – Phil Jul 06 '22 at 18:31
  • fyi, *"dplyr melt"* isn't a thing ... it's one of: `reshape2::melt`, `data.table::melt`, `reshape::melt`, or differently-named `tidyr::pivot_longer` or `tidyr::gather` (superseded). – r2evans Jul 06 '22 at 18:47

1 Answers1

1

reshape2

# library(reshape2)
reshape2::dcast(dat, name + GRADUATION_RATE ~ varname, value.var = "count")
#             name GRADUATION_RATE Bus Stops Childcare Centers
# 1 Example Name 1              13       251                87
# 2 Example Name 2              14       300                60

(This also works with data.table::dcast if you have a data.table-object.)

tidyr

# library(tidyr)
tidyr::pivot_wider(dat, names_from = "varname", values_from = "count")
# # A tibble: 2 x 4
#   name           GRADUATION_RATE `Bus Stops` `Childcare Centers`
#   <chr>                    <dbl>       <int>               <int>
# 1 Example Name 1              13         251                  87
# 2 Example Name 2              14         300                  60

Data

dat <- structure(list(name = c("Example Name 1", "Example Name 1", "Example Name 2", "Example Name 2"), GRADUATION_RATE = c(13, 13, 14, 14), varname = c("Bus Stops", "Childcare Centers", "Bus Stops", "Childcare Centers"), count = c(251L, 87L, 300L, 60L)), class = "data.frame", row.names = c(NA, -4L))
r2evans
  • 141,215
  • 6
  • 77
  • 149