Reputation: 939
I am trying to convert a tibble with a single row to turn it into a table with 2 columns.
Here's my example :
Many thanks in advance !
library(tibble)
mydf <- tribble(
~colA, ~colB, ~colC, ~colD,
"a", "1", "X", "2"
)
# What I want
# A tibble: 4 x 2
carac value
<chr> <chr>
colA a
colB 1
colC X
colD 2
Upvotes: 1
Views: 63
Reputation: 887028
We can use pivot_longer
from tidyr
to reshape the whole dataset into 'long' format with two columns
library(dplyr)
library(tidyr)
mydf %>%
pivot_longer(cols = everything(), names_to = 'carac')
-ouptut
# A tibble: 4 x 2
carac value
<chr> <chr>
1 colA a
2 colB 1
3 colC X
4 colD 2
In base R
, we could use stack
(if needed, set the column names differently)
stack(mydf)[2:1]
Upvotes: 2