通过添加带有动态名称和记录值的新列来展开数据框

发布于 2025-02-13 17:14:40 字数 795 浏览 0 评论 0原文

我当前的数据框是格式:

     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

我想扩展数据框,以便基于变量名称的计数创建新列,因此将有与唯一的varnames数量一样多的新列。因此,上述预期输出将是:

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

我猜这就像Dplyr融化的相反,但我不确定该怎么做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2025-02-20 17:14:41

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

(这也可以与data.table :: dcast一起使用,如果您有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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文