library(data.table)
library(tidyverse)
anscombe
#> x1 x2 x3 x4 y1 y2 y3 y4
#> 1 10 10 10 8 8.04 9.14 7.46 6.58
#> 2 8 8 8 8 6.95 8.14 6.77 5.76
#> 3 13 13 13 8 7.58 8.74 12.74 7.71
#> 4 9 9 9 8 8.81 8.77 7.11 8.84
#> 5 11 11 11 8 8.33 9.26 7.81 8.47
#> 6 14 14 14 8 9.96 8.10 8.84 7.04
#> 7 6 6 6 8 7.24 6.13 6.08 5.25
#> 8 4 4 4 19 4.26 3.10 5.39 12.50
#> 9 12 12 12 8 10.84 9.13 8.15 5.56
#> 10 7 7 7 8 4.82 7.26 6.42 7.91
#> 11 5 5 5 8 5.68 4.74 5.73 6.89
anscombe %>%
rownames_to_column("id") %>%
as.data.table() %>%
melt.data.table(
variable.name = "ex",
id.vars = "id",
measure.vars = list(x = c("x1", "x2", "x3", "x4"),
y = c("y1", "y2", "y3", "y4")))
#> id ex x y
#> 1: 1 1 10 8.04
#> 2: 2 1 8 6.95
#> 3: 3 1 13 7.58
#> 4: 4 1 9 8.81
#> 5: 5 1 11 8.33
#> 6: 6 1 14 9.96
#> 7: 7 1 6 7.24
#> 8: 8 1 4 4.26
#> 9: 9 1 12 10.84
#> 10: 10 1 7 4.82
#> 11: 11 1 5 5.68
#> 12: 1 2 10 9.14
#> 13: 2 2 8 8.14
#> 14: 3 2 13 8.74
#> 15: 4 2 9 8.77
#> 16: 5 2 11 9.26
#> 17: 6 2 14 8.10
#> 18: 7 2 6 6.13
#> 19: 8 2 4 3.10
#> 20: 9 2 12 9.13
#> 21: 10 2 7 7.26
#> 22: 11 2 5 4.74
#> 23: 1 3 10 7.46
#> 24: 2 3 8 6.77
#> 25: 3 3 13 12.74
#> 26: 4 3 9 7.11
#> 27: 5 3 11 7.81
#> 28: 6 3 14 8.84
#> 29: 7 3 6 6.08
#> 30: 8 3 4 5.39
#> 31: 9 3 12 8.15
#> 32: 10 3 7 6.42
#> 33: 11 3 5 5.73
#> 34: 1 4 8 6.58
#> 35: 2 4 8 5.76
#> 36: 3 4 8 7.71
#> 37: 4 4 8 8.84
#> 38: 5 4 8 8.47
#> 39: 6 4 8 7.04
#> 40: 7 4 8 5.25
#> 41: 8 4 19 12.50
#> 42: 9 4 8 5.56
#> 43: 10 4 8 7.91
#> 44: 11 4 8 6.89
#> id ex x y
Last active
July 20, 2018 01:29
-
-
Save BrunoGrandePhD/874ea4ca06548f4f7d901aa30d489529 to your computer and use it in GitHub Desktop.
Gather (melt) on multiple columns at once
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment