How to switch a row dimension to a column dimension in R? -


this question has answer here:

i have df looks below.

     year month cont  1     2011     apr 1376  2     2012     apr 1232  3     2013     apr 1360  4     2014     apr 1294  5     2015     apr 1344  6     2011     aug 1933  7     2012     aug 1930  8     2013     aug 1821  9     2014     aug 1845  10    2015     aug 1855 

so question how can switch rows in "month" column. result should this.

    cont  apr  aug 1   2011    1376    1933  2   2012    1232    1930  3   2013    1360    1821  4   2014    1294    1845  5   2015    1344    1855 

you can use reshape2:

library(reshape2) dcast(df, year~month, value.var="cont") 

or tidyr:

library(tidyr) spread(df, month, cont) 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -