We can use pivot_longer
from tidyr
library(dplyr)
library(tidyr)
dt %>%
pivot_longer(cols = Apples:Oranges, names_to = 'Type',
values_to = 'Values') %>%
arrange(Year, Type)
-output
# A tibble: 6 x 5
# Year Month Location Type Values
# <dbl> <chr> <chr> <chr> <dbl>
#1 2020 Jan Store_1 Apples 100
#2 2020 Jan Store_1 Apples 150
#3 2020 Feb Store_2 Apples 120
#4 2020 Jan Store_1 Oranges 50
#5 2020 Jan Store_1 Oranges 70
#6 2020 Feb Store_2 Oranges 50
solved Unpivot or transpose columns into rows R [duplicate]