id <- rep(c(1, 2, 3, 4), 3)
name <- rep(c("a", "b", "c", "d"), 3)
variable_a <- c(1:4, 9:12, 17:20)
variable_b <- c(5:8, 13:16, 21:24)
test1 <- data.frame(id, name, variable_a, variable_b)
I have a data set that looks like this:
id <- c(1, 2, 3, 4)
name <- c("a", "b", "c", "d")
variable_1a <- c(1, 2, 3, 4)
variable_1b <- c(5, 6, 7, 8)
variable_2a <- c(9, 10, 11, 12)
variable_2b <- c(13, 14, 15, 16)
variable_3a <- c(17, 18, 19, 20)
variable_3b <- c(21, 22, 23, 24)
test <- data.frame(id, name,
variable_1a, variable_1b,
variable_2a, variable_2b,
variable_3a, variable_3b)
id name variable_1a variable_1b variable_2a variable_2b variable_3a variable_3b
1 1 a 1 5 9 13 17 21
2 2 b 2 6 10 14 18 22
3 3 c 3 7 11 15 19 23
4 4 d 4 8 12 16 20 24
And I am trying to copy-paste each column pair (1a/1b, 2a/2b, 3a/3b) on top of each other, while repeating the id and names columns. In sum, I would like a dataset like that:
id <- rep(c(1, 2, 3, 4), 3)
name <- rep(c("a", "b", "c", "d"), 3)
variable_a <- c(1:4, 9:12, 17:20)
variable_b <- c(5:8, 13:16, 21:24)
test1 <- data.frame(id, name, variable_a, variable_b)
id name variable_a variable_b
1 1 a 1 5
2 2 b 2 6
3 3 c 3 7
4 4 d 4 8
5 1 a 9 13
6 2 b 10 14
7 3 c 11 15
8 4 d 12 16
9 1 a 17 21
10 2 b 18 22
11 3 c 19 23
12 4 d 20 24
I have tried various options of melt and row binds but I could not make them work in this column pair fashion. Any idea on would be a useful command for that? Thanks!