I have a dataframe named a whose column names are x1,y1,x2,y2,x3,y3. And how can I reshape it to a dataframe with column names as x,y without using loop, using tidyr::gather and tidyr::spread is preferred.
i.e.
a<-structure(list(X1 = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L, 0L, 0L),
Y1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 0L, 0L, 0L), X2 = 0:9,
Y2 = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 8L, 7L), X3 = c(0L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 0L, 0L), Y3 = c(3L, 4L, 5L, 6L,
7L, 8L, 9L, 8L, 0L, 0L)), .Names = c("X1", "Y1", "X2", "Y2",
"X3", "Y3"), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10"))
want to be:
structure(list(x = c(0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 0, 0), y = c(1, 2,
3, 4, 5, 6, 7, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 3, 4, 5,
6, 7, 8, 9, 8, 0, 0)), .Names = c("x", "y"), row.names = c(NA,
-30L), class = "data.frame")