I have four columns:
Year | I | I | I |
---|---|---|---|
1951 | 4 | 6 | 7 |
1952 | 8 | 0 | 3 |
1953 | 3 | 2 | 9 |
how do I combine them into two like that?:
Year | I |
---|---|
1951 | 4 |
1951 | 6 |
1951 | 7 |
1952 | 8 |
1952 | 0 |
1952 | 3 |
1953 | 3 |
1953 | 2 |
1953 | 9 |
I have four columns:
Year | I | I | I |
---|---|---|---|
1951 | 4 | 6 | 7 |
1952 | 8 | 0 | 3 |
1953 | 3 | 2 | 9 |
how do I combine them into two like that?:
Year | I |
---|---|
1951 | 4 |
1951 | 6 |
1951 | 7 |
1952 | 8 |
1952 | 0 |
1952 | 3 |
1953 | 3 |
1953 | 2 |
1953 | 9 |
I am not completely sure what your format is, but probably you want something like this by using pivot_longer
to make your table from wide to long:
df <- data.frame(Year = c(1951, 1952, 1953),
v1 = c(4,8,3),
v2 = c(6,0,2),
v3 = c(7,3,9))
library(tidyr)
df %>%
pivot_longer(!Year, names_to = "variable", values_to = "values")
Output:
# A tibble: 9 × 3
Year variable values
<dbl> <chr> <dbl>
1 1951 v1 4
2 1951 v2 6
3 1951 v3 7
4 1952 v1 8
5 1952 v2 0
6 1952 v3 3
7 1953 v1 3
8 1953 v2 2
9 1953 v3 9