[英]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 ![]() |
gp_2011 ![]() |
gp_2012 ![]() |
gp_2013 ![]() |
ge_2011 ![]() |
ge_2012 ![]() |
ge_2013 ![]() |
---|---|---|---|---|---|---|
01 ![]() |
25 ![]() |
NA![]() |
NA![]() |
12 ![]() |
NA![]() |
NA![]() |
01 ![]() |
NA![]() |
32 ![]() |
NA![]() |
NA![]() |
22 ![]() |
NA![]() |
02 ![]() |
95 ![]() |
NA![]() |
NA![]() |
62 ![]() |
NA![]() |
NA![]() |
02 ![]() |
NA![]() |
73 ![]() |
NA![]() |
NA![]() |
41 ![]() |
NA![]() |
02 ![]() |
NA![]() |
NA![]() |
68 ![]() |
NA![]() |
NA![]() |
55 ![]() |
03 ![]() |
24 ![]() |
NA![]() |
NA![]() |
16 ![]() |
NA![]() |
NA![]() |
03 ![]() |
NA![]() |
34 ![]() |
NA![]() |
NA![]() |
22 ![]() |
NA![]() |
03 ![]() |
NA![]() |
NA![]() |
41 ![]() |
NA![]() |
NA![]() |
20 ![]() |
I want this:我要这个:
id ![]() |
gp_2011 ![]() |
gp_2012 ![]() |
gp_2013 ![]() |
ge_2011 ![]() |
ge_2012 ![]() |
ge_2013 ![]() |
---|---|---|---|---|---|---|
01 ![]() |
25 ![]() |
32 ![]() |
NA![]() |
12 ![]() |
22 ![]() |
NA![]() |
02 ![]() |
95 ![]() |
73 ![]() |
68 ![]() |
62 ![]() |
41 ![]() |
55 ![]() |
03 ![]() |
24 ![]() |
34 ![]() |
41 ![]() |
16 ![]() |
22 ![]() |
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.