【问题标题】:Pivot longer multiple columns while pivot wider others旋转更长的多列,同时旋转更宽的其他列
【发布时间】:2021-02-20 17:04:42
【问题描述】:

您好,我有一个每组 3-5 行的数据集,如下所示,我想将一些列以更长的格式放置,并以更宽的格式放置列。

下面的第一个数据集代表原始格式,我想将其转换为第二个。我使用了 pivot更宽的 cols = c("Jan", "Feb") 但我无法同时将 Type 列旋转得更长。

data <- as.data.frame(matrix(ncol=5, nrow=6))
colnames(data) <- c("names", "group", "Type", "Jan", "Feb")
data$names <- c("P1", "P1", "P1", "P2", "P2", "P2")
data$group <- "S"
data$Type <- c("Beg", "Middle", "End", "Beg", "Middle", "End")
data$Jan <- c(1, 2, 3, 10, 5, 15)
data$Feb <- c(5, 5, 10, 5, 2, 7)

    
   names group Type     Jan  Feb
1   P1    S    Beg       1   5
2   P1    S    Middle    2   5
3   P1    S    End       3   10
4   P2    S    Beg       10  5
5   P2    S    Middle    5   2
6   P2    S    End       15  7


data_transformed <- as.data.frame(matrix(ncol=6, nrow=4))
colnames(data_transformed) <- c("names", "group", "Month", "Beg", "Middle", "End")
data_transformed$names <- c("P1", "P1", "P2", "P2")
data_transformed$group <- "S"
data_transformed$Month <- c("Jan", "Feb")
data_transformed$Beg <- c(1, 10, 5, 5)
data_transformed$Middle <- c(2, 5, 5, 2)
data_transformed$End <- c(2, 15, 10, 7)

  names group Month   Beg Middle End
1   P1  S     Jan      1    2    2
2   P1  S     Feb      10   5    15
3   P2  S     Jan      5    5    10
4   P2  S     Feb      5    2    7

【问题讨论】:

    标签: r dplyr pivot data-manipulation


    【解决方案1】:

    在这里,我们需要一个pivot_longer + pivot_wider,即首先使用cols Jan 将其重新整形为“long”,然后将其重新整形为更宽的格式,并使用“Type”中的列名

    library(dplyr)
    library(tidyr)
    data %>%
         pivot_longer(cols = Jan:Feb, names_to = 'Month') %>% 
         pivot_wider(names_from = Type, values_from = value)
    

    -输出

    # A tibble: 4 x 6
    #  names group Month   Beg Middle   End
    #  <chr> <chr> <chr> <dbl>  <dbl> <dbl>
    #1 P1    S     Jan       1      2     3
    #2 P1    S     Feb       5      5    10
    #3 P2    S     Jan      10      5    15
    #4 P2    S     Feb       5      2     7
    

    或者使用recast 来自reshape2

    library(reshape2)
    recast(data, measure = c("Jan", "Feb"),
         names + group + variable ~ Type, values.var = 'value')
    

    【讨论】:

      【解决方案2】:

      data.table 选项使用 dcast + melt

      dcast(
        melt(
          setDT(data),
          id.vars = c("names", "group", "Type"),
          variable.name = "Month"
        ),
        names + group + Month ~ Type
      )
      

      给予

         names group Month Beg End Middle
      1:    P1     S   Jan   1   3      2
      2:    P1     S   Feb   5  10      5
      3:    P2     S   Jan  10  15      5
      4:    P2     S   Feb   5   7      2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-08
        • 2022-11-23
        • 1970-01-01
        • 2020-06-12
        • 2022-01-05
        • 1970-01-01
        • 2022-07-06
        • 2021-12-27
        相关资源
        最近更新 更多