简体   繁体   English

pivot_wider 不折叠行

[英]pivot_wider not collapsing rows

got a pretty basic question to ask unfortunately, I am trying to use a pivot_wider to make my data into a panel.不幸的是,有一个非常基本的问题要问,我正在尝试使用 pivot_wider 将我的数据放入面板中。

variable id reports gp & ge every year, the column t denotes the year.变量 id 每年报告 gp & ge,t 列表示年份。 I want a separate variable gp_t and ge_t for every year in the data(ie t =[2011 -2013])我希望数据中每年都有一个单独的变量 gp_t 和 ge_t(即 t =[2011 -2013])

When I use pivot_wider, I am getting the right number of columns, but the rows are not collapsing into each other as they have in the past when I have used this function.当我使用 pivot_wider 时,我得到了正确数量的列,但是当我使用这个函数时,这些行并没有像过去那样相互折叠。 Can someone please give me a hand?有人可以帮帮我吗? Because I am using t as the "names_from" for 2 different columns, I duplicated it... was this the wrong thing to do?因为我使用 t 作为 2 个不同列的“names_from”,所以我复制了它……这是错误的做法吗?

df$t2<-df$t

df<-df %>%
select(id,t,t2,gp,ge)%>%
group_by(id)%>%
pivot_wider(names_from = "t", names_prefix= "gp_" values_from = "gp")%>%
pivot_wider(names_from = "t2", names_prefix= "ge_" values_from = "ge")

NB: company 01 went out of business in 2013, this needs to be kept visible in the table if possible!注意:公司 01 于 2013 年倒闭,如果可能,这需要在表格中保持可见!

I am currently getting this:我目前得到这个:

id ID gp_2011 gp_2011 gp_2012 gp_2012 gp_2013 gp_2013 ge_2011 ge_2011 ge_2012 ge_2012 ge_2013 ge_2013
01 01 25 25 NA不适用 NA不适用 12 12 NA不适用 NA不适用
01 01 NA不适用 32 32 NA不适用 NA不适用 22 22 NA不适用
02 02 95 95 NA不适用 NA不适用 62 62 NA不适用 NA不适用
02 02 NA不适用 73 73 NA不适用 NA不适用 41 41 NA不适用
02 02 NA不适用 NA不适用 68 68 NA不适用 NA不适用 55 55
03 03 24 24 NA不适用 NA不适用 16 16 NA不适用 NA不适用
03 03 NA不适用 34 34 NA不适用 NA不适用 22 22 NA不适用
03 03 NA不适用 NA不适用 41 41 NA不适用 NA不适用 20 20

I want this:我要这个:

id ID gp_2011 gp_2011 gp_2012 gp_2012 gp_2013 gp_2013 ge_2011 ge_2011 ge_2012 ge_2012 ge_2013 ge_2013
01 01 25 25 32 32 NA不适用 12 12 22 22 NA不适用
02 02 95 95 73 73 68 68 62 62 41 41 55 55
03 03 24 24 34 34 41 41 16 16 22 22 20 20

Any help or handy hints greatly appreciated!非常感谢任何帮助或方便的提示!

Thanks!谢谢!

There is neither need to duplicate your t column nor to use two pivot_wider .既不需要复制t列,也不需要使用两个pivot_wider Instead you could achieve your desired result like so:相反,您可以像这样实现您想要的结果:

library(dplyr)
library(tidyr)

df %>%
  select(id, t, gp, ge) %>%
  pivot_wider(names_from = "t", values_from = c(gp, ge))
#> # A tibble: 3 × 7
#>   id    gp_2011 gp_2012 gp_2013 ge_2011 ge_2012 ge_2013
#>   <chr>   <int>   <int>   <int>   <int>   <int>   <int>
#> 1 01         25      32      NA      12      22      NA
#> 2 02         95      73      68      62      41      55
#> 3 03         24      34      41      16      22      20

DATA数据

df_wide <- data.frame(
                id = c("01", "02", "03"),
           gp_2011 = c(25L, 95L, 24L),
           gp_2012 = c(32L, 73L, 34L),
           gp_2013 = c(NA, 68L, 41L),
           ge_2011 = c(12L, 62L, 16L),
           ge_2012 = c(22L, 41L, 22L),
           ge_2013 = c(NA, 55L, 20L)
           )

library(tidyr)

df <- df_wide %>%
  pivot_longer(-id, names_to = c(".value", "t"), names_sep = "_")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM