Here, we show how to reshape, split and unsplit data objects in R using the reshape(), split() and unsplit() functions.

split() and unsplit() are from the "base" package, while reshape() is from the "stats" package.

Summary Functions for Reshaping or Splitting Data Objects in R
Function Usage
reshape() Reshape data
split() Split dataframe by column
unsplit() Unsplit dataframe

1 Reshape Data in R

One Column as Identity Variable

Here we show how to reshape data by one column.

Sample dataframe:

dtf = data.frame(Person = rep(c("Miz", "Don", "Shu", "Ria"), each = 2),
                 Treatment = rep(c("A", "B"), times = 4),
                 Point = rbinom(8, 20, 0.5))
dtf
  Person Treatment Point
1    Miz         A     7
2    Miz         B     8
3    Don         A     8
4    Don         B     9
5    Shu         A    12
6    Shu         B    12
7    Ria         A     6
8    Ria         B    10

Reshape based on the "Treatment" column by turning the "Treatment" column into multiple columns:

The variable of interest is set in the argument "v.names", the variable to split by is set in the argument "timevar", and the identifying variable is set in the argument "idvar".

dtf_wide = reshape(dtf, v.names = "Point", timevar = "Treatment",
                   idvar = "Person", direction = "wide")
dtf_wide
  Person Point.A Point.B
1    Miz       7       8
3    Don       8       9
5    Shu      12      12
7    Ria       6      10

Reshape to reset back to the initial form:

We can reset back to the initial form by changing the "direction" argument to "long".

reshape(dtf_wide, direction = "long")
      Person Treatment Point
Miz.A    Miz         A     7
Don.A    Don         A     8
Shu.A    Shu         A    12
Ria.A    Ria         A     6
Miz.B    Miz         B     8
Don.B    Don         B     9
Shu.B    Shu         B    12
Ria.B    Ria         B    10

Two Columns as Identity Variables

Here we show an example using two columns as the identity variables.

Sample dataframe:

dtf = data.frame(Person = rep(c("Miz", "Don"), each = 4),
                 Treatment = rep(c("A", "B"), each = 2, times = 2),
                 Dose = rep(c("Full", "Half"), times = 4),
                 Point = rbinom(8, 20, 0.5))
dtf
  Person Treatment Dose Point
1    Miz         A Full     8
2    Miz         A Half     8
3    Miz         B Full    12
4    Miz         B Half    10
5    Don         A Full     8
6    Don         A Half    10
7    Don         B Full    12
8    Don         B Half    10

Reshape by turning "Dose" into multiple columns:

The identity variables are "Person" and "Treatment".

reshape(dtf, v.names = "Point", idvar = c("Person", "Treatment"),
        timevar = "Dose", direction = "wide")
  Person Treatment Point.Full Point.Half
1    Miz         A          8          8
3    Miz         B         12         10
5    Don         A          8         10
7    Don         B         12         10

2 Split and Unsplit Data Objects in R

Here we show how to split a dataframe into a list of multiple dataframes by a columm.

Sample dataframe:

dtf = data.frame(Person = rep(c("Gab", "Mia", "Kae", "Jip"), each = 2),
                 Treatment = rep(c("A", "B"), times = 4),
                 Point = rpois(8, 30))
dtf
  Person Treatment Point
1    Gab         A    24
2    Gab         B    25
3    Mia         A    37
4    Mia         B    36
5    Kae         A    25
6    Kae         B    39
7    Jip         A    26
8    Jip         B    24

Split by the "Treatment" column into multiple objects:

dtf_split = split(dtf, dtf$Treatment)
dtf_split
$A
  Person Treatment Point
1    Gab         A    24
3    Mia         A    37
5    Kae         A    25
7    Jip         A    26

$B
  Person Treatment Point
2    Gab         B    25
4    Mia         B    36
6    Kae         B    39
8    Jip         B    24

Unsplit by the "Treatment" factor back to the initial dataframe:

unsplit(dtf_split, dtf$Treatment)
  Person Treatment Point
1    Gab         A    24
2    Gab         B    25
3    Mia         A    37
4    Mia         B    36
5    Kae         A    25
6    Kae         B    39
7    Jip         A    26
8    Jip         B    24

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes