Here is the type of transformation I am trying to do.
CodePudding user response:
library(tidyverse)
df <- tibble(
id = c(123, 127),
first_name = c("john", "andrew"),
last_name = c("smith", "jones"),
orange_price = c(2.4, 3.2),
apple_price = c(2, 1),
kiwi_price = c(1.25, 0.75)
)
df %>%
pivot_longer(names_to = "fruit",
values_to = "price",
cols = c(orange_price, apple_price, kiwi_price))
#> # A tibble: 6 x 5
#> id first_name last_name fruit price
#> <dbl> <chr> <chr> <chr> <dbl>
#> 1 123 john smith orange_price 2.4
#> 2 123 john smith apple_price 2
#> 3 123 john smith kiwi_price 1.25
#> 4 127 andrew jones orange_price 3.2
#> 5 127 andrew jones apple_price 1
#> 6 127 andrew jones kiwi_price 0.75
Created on 2022-06-28 by the reprex package (v2.0.1)