R语言 枢轴较长的多列,而枢轴较宽的其他列

qlfbtfca  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(155)

你好,我有一个数据集,每组3-5行,如下所示,我想把一些列在一个较长的格式,并在一个更广泛的格式列。
下面的第一个数据集表示原始格式,我想将其转换为第二个数据集。我使用了pivot wider = 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

字符串

hfyxw5xn

hfyxw5xn1#

在这里,我们需要一个pivot_longer + pivot_wider,即首先将col s Jan整形为“long”,然后将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


或使用reshape2中的recast

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

lnlaulya

lnlaulya2#

使用dcast + meltdata.table选项

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

wxclj1h5

wxclj1h53#

这已经晚了好几年,但是procs包中的proc_transpose(当时可能还不存在于CRAN上)可以按组转置。
代码下面指定分组依据的列(by=)。id列(id=)是成为输出数据框中列名的输入列。此处无需指定,因为在移除两个分组列后只剩下一个字符列,并且默认情况下假定单个字符列为id列。新的输出中保存输入中列名的列(name=)被指定为"Month",但如果默认值NAME足够,则可以省略。

library(procs)

proc_transpose(data, by = c("names", "group"), name = "Month")
##   names group Month Beg Middle End
## 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

字符串

相关问题