如何将数据帧从长格式拆分为宽格式?

时间:2018-02-13 15:47:25

标签: r dataframe reshape

我有一个有很多行的数据框,我希望将它们并排显示在多个列上。

这是我的数据框的一个例子

my.df <- data.frame(BDS_ID = 4567, ID_Monet = "BG0065W", CAP = "2xxxx0", Indirizzo = "Via A. 3",
                    Comune = "Neder", Tensione = "Monofase", Stato = "Italy", Specifica = 1,
                    Importo = 0, Cod_POD = "IT0098765", Rif_tec = "NA", Lavori = 1)
my.df <- t(my.df)
my.df <- cbind(row.names(my.df),my.df)

如果我想将数据帧分成两半并将两半放在一起,我可以使用以下语句:

my.df <- cbind(my.df[c(1:(nrow(my.df)/2)),], my.df[c(((nrow(my.df)/2)+1):nrow(my.df)),])

但是如果我想创建一个参数,我声明要显示多少列并创建一个函数,根据参数,在每列中插入正确的行数我该怎么办?

基本上我想创建一个这样的参数:

number_of_columns <- 4

然后创建一个自动划分数据框的函数?

有谁知道怎么做?提前致谢

1 个答案:

答案 0 :(得分:2)

您可以尝试tidyverse解决方案

library(tidyverse)
number_of_columns <- 4
my.df %>% 
  as.tibble() %>% 
  gather(k, v) %>% 
  mutate(n1=gl(number_of_columns, n()/number_of_columns)) %>% 
  group_by(n1) %>% 
  mutate(n2=1:n()) %>% 
  select(-k) %>% 
  spread(n1, v)
# A tibble: 6 x 5
     n2       `1`       `2`      `3`       `4`
* <int>     <chr>     <chr>    <chr>     <chr>
1     1    BDS_ID     Stato     4567     Italy
2     2  ID_Monet Specifica  BG0065W         1
3     3       CAP   Importo   2xxxx0         0
4     4 Indirizzo   Cod_POD Via A. 3 IT0098765
5     5    Comune   Rif_tec    Neder        NA
6     6  Tensione    Lavori Monofase         1

使用函数的工作示例

foo <- function(x) {
  my.df %>% 
    as.tibble() %>% 
    gather(k, v) %>% 
    mutate(n1=gl(x, n()/x)) %>% 
    group_by(n1) %>% 
    mutate(n2=1:n()) %>% 
    select(-k) %>% 
    spread(n1, v)
}
c(2,3,4,6) %>% 
  map(~foo(.))
[[1]]
# A tibble: 12 x 3
      n2       `1`       `2`
 * <int>     <chr>     <chr>
 1     1    BDS_ID      4567
 2     2  ID_Monet   BG0065W
 3     3       CAP    2xxxx0
 4     4 Indirizzo  Via A. 3
 5     5    Comune     Neder
 6     6  Tensione  Monofase
 7     7     Stato     Italy
 8     8 Specifica         1
 9     9   Importo         0
10    10   Cod_POD IT0098765
11    11   Rif_tec        NA
12    12    Lavori         1

[[2]]
# A tibble: 8 x 4
     n2       `1`      `2`       `3`
* <int>     <chr>    <chr>     <chr>
1     1    BDS_ID  Importo     Neder
2     2  ID_Monet  Cod_POD  Monofase
3     3       CAP  Rif_tec     Italy
4     4 Indirizzo   Lavori         1
5     5    Comune     4567         0
6     6  Tensione  BG0065W IT0098765
7     7     Stato   2xxxx0        NA
8     8 Specifica Via A. 3         1

[[3]]
# A tibble: 6 x 5
     n2       `1`       `2`      `3`       `4`
* <int>     <chr>     <chr>    <chr>     <chr>
1     1    BDS_ID     Stato     4567     Italy
2     2  ID_Monet Specifica  BG0065W         1
3     3       CAP   Importo   2xxxx0         0
4     4 Indirizzo   Cod_POD Via A. 3 IT0098765
5     5    Comune   Rif_tec    Neder        NA
6     6  Tensione    Lavori Monofase         1

[[4]]
# A tibble: 4 x 7
     n2       `1`       `2`     `3`      `4`      `5`       `6`
* <int>     <chr>     <chr>   <chr>    <chr>    <chr>     <chr>
1     1    BDS_ID    Comune Importo     4567    Neder         0
2     2  ID_Monet  Tensione Cod_POD  BG0065W Monofase IT0098765
3     3       CAP     Stato Rif_tec   2xxxx0    Italy        NA
4     4 Indirizzo Specifica  Lavori Via A. 3        1         1