I have the following dummy data:
library(dplyr)
library(tidyr)
library(reshape2)
dt <- expand.grid(Year = 1990:2014, Product=LETTERS[1:8], Country = paste0(LETTERS, "I")) %>% select(Product, Country, Year)
dt$value <- rnorm(nrow(dt))
I pick two product-country combinations
sdt <- dt %>% filter((Product == "A" & Country == "AI") | (Product == "B" & Country =="EI"))
and I want to see the values side by side for each combination. I can do this with dcast
:
sdt %>% dcast(Year ~ Product + Country)
Is it possible to do this with spread
from the package tidyr?