在 R 中旋转更宽的一行

Pivot wider to one row in R

这是我正在使用的示例代码

  library(dplyr)

  naics <- c("000000","000000",123000,123000)
  year <- c(2020,2021,2020,2021)
  January <- c(250,251,6,9)
  February <- c(252,253,7,16)
  March <- c(254,255,8,20)

  sample2 <- data.frame (naics, year, January, February, March)

这是预期的结果

                 Jan2020        Feb2020    March2020      Jan2021        Feb2021   March2021   
 000000            250             252          254          251            253           255
 123000             6                7           8            9              16            20

这是用 pivot_wider 完成的还是更复杂?

我们通过选择 values_from 和月份列来使用 pivot_widernames_from 为 'year',然后更改 names_glue 中的列名格式和如果需要,将 'naics' 转换为带有 column_to_rownames 的行名称(来自 tibble

library(tidyr)
library(tibble)
pivot_wider(sample2, names_from = year, values_from = January:March, 
      names_glue = "{substr(.value, 1, 3)}{year}")%>% 
   column_to_rownames('naics')

-输出

       Jan2020 Jan2021 Feb2020 Feb2021 Mar2020 Mar2021
000000     250     251     252     253     254     255
123000       6       9       7      16       8      20

这比@akrun 的回答走的路要长。我将把它留在这里,以防它有助于对所采取的步骤有更多的直觉。否则,@akrun 的答案更节省资源。

sample2 %>% 
   tidyr::pivot_longer(-c(naics, year), names_to = "month", 
                       values_to = "value") %>% 
   mutate(Month=paste0(month, year)) %>% 
   select(-year, - month) %>% 
   tidyr::pivot_wider(names_from = Month,values_from = value)


   # A tibble: 2 x 7
  naics  January2020 February2020 March2020 January2021 February2021
  <chr>        <dbl>        <dbl>     <dbl>       <dbl>        <dbl>
1 000000         250          252       254         251          253
2 123000           6            7         8           9           16
# ... with 1 more variable: March2021 <dbl>

使用来自 BaseR

reshape 函数
reshape(sample2, dir = "wide", sep="",
        idvar = "naics", 
        timevar = "year",
        new.row.names = unique(naics))[,-1]

#        January2020 February2020 March2020 January2021 February2021 March2021
# 000000         250          252       254         251          253       255
# 123000           6            7         8           9           16        20