First, here is the data tbl_df
(simplified) I am using :
> mytbldf
Source: local data frame [6 x 5]
iso2c country year var1 var2
1 BI Burundi 2011 4.486265 6.693711
2 BI Burundi 2012 3.939242 5.330326
3 BI Burundi 2013 4.286439 5.747370
4 UG Uganda 2011 3.998849 10.025680
5 UG Uganda 2012 4.606198 13.416311
6 UG Uganda 2013 4.746322 15.981362
I want to spread (in tidyr wording) the year variable over var1
and var2
.
After some (...) iterations, I found a syntax that works:
> recast(mytbldf, iso2c + country ~ variable + year, measure.var = c("var1","var2"))
iso2c country var1_2011 var1_2012 var1_2013 var2_2011 var2_2012 var2_2013
1 BI Burundi 4.486265 3.939242 4.286439 6.693711 5.330326 5.74737
2 UG Uganda 3.998849 4.606198 4.746322 10.025680 13.416311 15.98136
Three questions:
1) if I don't specify measure.var =
, I get the following error:
> recast(mytbldf, iso2c + country ~ variable + year)
Using iso2c, country as id variables
Error in eval(expr, envir, enclos) : object 'year' not found
Why is that? From recast
's man, I assumed it would take measure.var
as all other variables?
2) So, is there a way to avoid specifying measure.var
? In my real case, there are too many variables with too long names for explicitly specifying them.
3) Is there a better/simpler way to do that using reshape2
or tidyr
that I am missing?