R 透视数据框以重塑

问题描述 投票:0回答:1

我有两个数据框

Category = c("New", "Fixed" ,"Regular")
`Jan 2014` = c(-418518172, 24425087,0.3016606 )
`Feb 2014`=c(-168947846,17919651,-0.7212493)
`Mar 2014`=c(-168846,17544,-0.775493)
df1 <- data.frame(Category, `Jan 2014`, `Feb 2014`,`Mar 2014`, check.names = FALSE)
Category = c("New","Fixed" ,"Regular")
`Jan 2014` = c(-8172,-8524,0.5060900)
`Feb 2014`=c(-1686,19929,-0.85059894)
`Mar 2014`=c(-2424246,1232324,-0.7217593)
df2 <- data.frame(Category, `Jan 2014`, `Feb 2014`, `Mar 2014`,check.names = FALSE)
calculate_df <- cbind(df1,(df1[-1]/df2[-1])*100)

经过一些计算后,我试图重塑它,使所有日期都成为

Date
列,然后每种类型的
Category
New
Fixed
Regular
都位于单独的列下,并具有各自的值。

reshape_df <- calculate_df %>% 
  pivot_longer(
    cols = !Category, 
    names_to = "Date", 
    values_to = "Values"
  )

calculate_df

电流输出

 Category Date       Values
   <chr>    <chr>       <dbl>
 1 New      Jan 2014 -4.19e+8
 2 New      Feb 2014 -1.69e+8
 3 New      Mar 2014 -1.69e+5
 4 New      Jan 2014  5.12e+6
 5 New      Feb 2014  1.00e+7
 6 New      Mar 2014  6.96e+0
 7 Fixed    Jan 2014  2.44e+7
 8 Fixed    Feb 2014  1.79e+7
 9 Fixed    Mar 2014  1.75e+4
10 Fixed    Jan 2014 -2.87e+5
11 Fixed    Feb 2014  8.99e+4
12 Fixed    Mar 2014  1.42e+0
13 Regular  Jan 2014  3.02e-1
14 Regular  Feb 2014 -7.21e-1
15 Regular  Mar 2014 -7.75e-1
16 Regular  Jan 2014  5.96e+1
17 Regular  Feb 2014  8.48e+1
18 Regular  Mar 2014  1.07e+2

怎样才能达到预期的效果?

预期输出

 Date       Category    Values  Category  Values   Category  Values   

 1 Jan 2014    New      -4.19e+8 Fixed   2.44e+7   Regular   3.02e-1
 2 Feb 2014    New      -1.69e+8 Fixed   1.79e+7   Regular   -7.21e-1
 3 Mar 2014    New      -1.69e+5 Fixed   1.75e+4   Regular   -7.75e-1
 4 Jan 2014    New      5.12e+6  Fixed   -2.87e+5  Regular   5.96e+1
 5 Feb 2014    New      1.00e+7  Fixed   8.99e+4   Regular   8.48e+1
 6 Mar 2014    New      6.96e+0  Fixed   1.42e+0   Regular   1.07e+2

r dataframe dplyr pivot data-wrangling
1个回答
0
投票

在基础R中,您可以从这里开始并找到一种不太冗长的方法。 请注意,我似乎不清楚为什么宽比长更好。除了发布之外,重复的列名称不是一个好习惯。

xyzzy = split(reshape_df, reshape_df$Category) 
xyzzy[[1]] = xyzzy[[1]][c("Date", "Category", "Values")]
xyzzy[-1] = lapply(xyzzy[-1], \(x) transform(x, Date = NULL))
do.call(cbind, xyzzy) |> setNames(c("Date", rep(c("Category", "Values"), 3)))

给予

      Date Category        Values Category        Values Category      Values
1 Jan 2014    Fixed  2.442509e+07      New -4.185182e+08  Regular   0.3016606
2 Feb 2014    Fixed  1.791965e+07      New -1.689478e+08  Regular  -0.7212493
3 Mar 2014    Fixed  1.754400e+04      New -1.688460e+05  Regular  -0.7754930
4 Jan 2014    Fixed -2.865449e+05      New  5.121368e+06  Regular  59.6061175
5 Feb 2014    Fixed  8.991746e+04      New  1.002063e+07  Regular  84.7931106
6 Mar 2014    Fixed  1.423652e+00      New  6.964887e+00  Regular 107.4448227
© www.soinside.com 2019 - 2024. All rights reserved.