Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd panel data example to vignette? #577
Comments
This comment has been minimized.
This comment has been minimized.
This is very similar to the anscombe example! The key is overwriting the auto generated library(tidyr)
library(dplyr, warn.conflicts = FALSE)
d <- data.frame(
x = 1:4,
y1 = rnorm(4),
y2 = rnorm(4),
z1 = rep(3,4),
z2 = rep(-2,4),
a = c(1,1,0,0),
b = c(0,1,1,1)
)
spec <- pivot_long_spec(d, c(y1, y2, z1, z2)) %>%
separate(name, c(".value", "time"), 1, convert = TRUE)
pivot_long(d, spec = spec)
#> # A tibble: 8 x 6
#> x a b time y z
#> <int> <dbl> <dbl> <int> <dbl> <dbl>
#> 1 1 1 0 1 -0.808 3
#> 2 1 1 0 2 0.491 -2
#> 3 2 1 1 1 -1.61 3
#> 4 2 1 1 2 -1.04 -2
#> 5 3 0 1 1 0.809 3
#> 6 3 0 1 2 2.26 -2
#> 7 4 0 1 1 -0.389 3
#> 8 4 0 1 2 -1.06 -2
reshape(
d,
varying = list(c("y1", "y2"), c("z1", "z2")),
v.names = c("y", "z"),
idvar = "x",
direction = "long"
) %>%
as_tibble() %>%
arrange(x, a, b)
#> # A tibble: 8 x 6
#> x a b time y z
#> <int> <dbl> <dbl> <int> <dbl> <dbl>
#> 1 1 1 0 1 -0.808 3
#> 2 1 1 0 2 0.491 -2
#> 3 2 1 1 1 -1.61 3
#> 4 2 1 1 2 -1.04 -2
#> 5 3 0 1 1 0.809 3
#> 6 3 0 1 2 2.26 -2
#> 7 4 0 1 1 -0.389 3
#> 8 4 0 1 2 -1.06 -2 Created on 2019-03-20 by the reprex package (v0.2.1.9000) |
This comment has been minimized.
This comment has been minimized.
(BTW I think this example would be a little easier to understand if Given that |
leeper commentedMar 19, 2019
Would be great to have an example with a mix of time-variant and time-invariant variables for
gather()
orpivot_long()
or both. basically what's the tidyr equivalent of: