Hello,
I have a data like this:
I'd like to convert the variable column in multiple columns, like this:
Can someone help me?
Hello,
I have a data like this:
I'd like to convert the variable column in multiple columns, like this:
Can someone help me?
Hi @Gabbi,
You need an ID column that uniquely identifies which values of age
, name
, and height
correspond to a single row in the desired output. With that, you can achieve this transformation with the following code:
library(tidyverse)
data <-
tribble(
~variable, ~value,
"age", "23",
"age", "44",
"age", "21",
"name", "a",
"name", "b",
"name", "c",
"height", "1",
"height", "2",
"height", "3"
)
data %>%
mutate(id = rep(1:3, 3)) %>%
pivot_wider(
id_cols = id,
names_from = variable,
values_from = value
)
#> # A tibble: 3 x 4
#> id age name height
#> <int> <chr> <chr> <chr>
#> 1 1 23 a 1
#> 2 2 44 b 2
#> 3 3 21 c 3
It works, thank you!! =)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.