Lorenzo Benassi
Lorenzo Benassi

Reputation: 621

How to split dataframe from long to wide format?

I have a dataframe that has many lines and I would like to display it on multiple columns side by side.

This is an example of mine dataframe

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)

If I want to split my dataframe in half and place the two halves together I can use this statement:

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

But if I wanted to create a parameter where I declare how many columns to display and create a function that, according to the parameter, insert in each column the correct number of rows how can I do?

Basically I want create a parameter like this:

number_of_columns <- 4

and then create a function that automatically divides the data frame?

Does anyone know how it can be done? Thanks in advance

Upvotes: 1

Views: 171

Answers (1)

Roman
Roman

Reputation: 17648

You can try a tidyverse solution

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

working example using a function

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

Upvotes: 2

Related Questions