Example of an wide data -
> wide <- data.frame(
+ ID = 1:6,
+ Gender = rep(c("F","M"),each = 3),
+ Control = round(runif(6,1,5)),
+ Exp1 = round(runif(6,1,5)),
+ Exp2 = round(runif(6,1,5))
+ )
> wide ID Gender Control Exp1 Exp2
1 1 F 3 4 4
2 2 F 4 2 3
3 3 F 2 2 1
4 4 M 5 3 3
5 5 M 3 4 2
6 6 M 5 3 3
Using gather() function from tidyr package to transform it to long format -
> tidyr::gather(data = wide,
+ key = Type, value = Value,
+ Control, Exp1, Exp2,
+ factor_key = T # If TRUE, key values will be stored as a factor
+ ) ID Gender Type Value
1 1 F Control 3
2 2 F Control 4
3 3 F Control 2
4 4 M Control 5
5 5 M Control 3
6 6 M Control 5
7 1 F Exp1 4
8 2 F Exp1 2
9 3 F Exp1 2
10 4 M Exp1 3
11 5 M Exp1 4
12 6 M Exp1 3
13 1 F Exp2 4
14 2 F Exp2 3
15 3 F Exp2 1
16 4 M Exp2 3
17 5 M Exp2 2
18 6 M Exp2 3
Another way to do this -
> tidyr::gather(data = wide,
+ key = Type, value = Value,
+ -Gender, - ID, # to use all columns except these
+ factor_key = T
+ ) ID Gender Type Value
1 1 F Control 3
2 2 F Control 4
3 3 F Control 2
4 4 M Control 5
5 5 M Control 3
6 6 M Control 5
7 1 F Exp1 4
8 2 F Exp1 2
9 3 F Exp1 2
10 4 M Exp1 3
11 5 M Exp1 4
12 6 M Exp1 3
13 1 F Exp2 4
14 2 F Exp2 3
15 3 F Exp2 1
16 4 M Exp2 3
17 5 M Exp2 2
18 6 M Exp2 3
gather_() can be used to programmatically do the whole thing. The difference is the column names need to be supplied as strings in quotation instead of bare column names.
> # To get help
> # ?melt.data.frame
> reshape2::melt(data = wide,
+ id.vars = c("ID","Gender"),
+ measure.vars = c("Control","Exp1","Exp2"),
+ variable.name = "Group",
+ value.name = "Values") ID Gender Group Values
1 1 F Control 3
2 2 F Control 4
3 3 F Control 2
4 4 M Control 5
5 5 M Control 3
6 6 M Control 5
7 1 F Exp1 4
8 2 F Exp1 2
9 3 F Exp1 2
10 4 M Exp1 3
11 5 M Exp1 4
12 6 M Exp1 3
13 1 F Exp2 4
14 2 F Exp2 3
15 3 F Exp2 1
16 4 M Exp2 3
17 5 M Exp2 2
18 6 M Exp2 3
Example of a data in long format -
> long <- tidyr::gather(data = wide,
+ key = Type, value = Value,
+ -Gender, - ID,
+ factor_key = T
+ )
> long ID Gender Type Value
1 1 F Control 3
2 2 F Control 4
3 3 F Control 2
4 4 M Control 5
5 5 M Control 3
6 6 M Control 5
7 1 F Exp1 4
8 2 F Exp1 2
9 3 F Exp1 2
10 4 M Exp1 3
11 5 M Exp1 4
12 6 M Exp1 3
13 1 F Exp2 4
14 2 F Exp2 3
15 3 F Exp2 1
16 4 M Exp2 3
17 5 M Exp2 2
18 6 M Exp2 3
> tidyr::spread(data = long,
+ key = Type, value = Value) ID Gender Control Exp1 Exp2
1 1 F 3 4 4
2 2 F 4 2 3
3 3 F 2 2 1
4 4 M 5 3 3
5 5 M 3 4 2
6 6 M 5 3 3
> reshape2::dcast(data = long,
+ formula = ID+Gender ~ Type,
+ value.var = "Value") ID Gender Control Exp1 Exp2
1 1 F 3 4 4
2 2 F 4 2 3
3 3 F 2 2 1
4 4 M 5 3 3
5 5 M 3 4 2
6 6 M 5 3 3