Transforming vertical Data to a horizontal format in R -


this conceptual question. have data in sql express database loading r via odbc package. data format is:

name, symbol, number of days 1/1/2015, closing price.

the main company, tmc, 1, 12 xyz company, xyz, 1, 233 1 company, toc, 1, 56 2 company, ttc, 1, 88  main company,tmc, 2, 11.5 xyz company, xyz, 2, 232 1 company, toc, 2, 59 2 company, ttc, 2, 89 

my question is: there r package can transform data being in vertical format horizontal? ie

name, symbol, price lag1, price lag2

the main company, tmc, 12, 11.5 xyz company, xyz, 233, 232 1 company, toc, 56,59 2 company, ttc, 88,89 

or should try sql code?

we use dcast reshape2 or spread tidyr. before reshaping, may better change lengthy column names spaces no space column names. specify value column in value.var in dcast.

library(reshape2) colnames(df1)[3:4] <- c('numberofdays', 'closing_price') dcast(df1, name+symbol~paste0('lag', numberofdays),                    value.var='closing_price') #              name symbol lag1  lag2 #1 main company    tmc   12  11.5 #2  1 company    toc   56  59.0 #3  2 company    ttc   88  89.0 #4  xyz company    xyz  233 232.0 

a similar option using dplyr/tidyr mutate 'numberofdays' pasteing 'lag' string , convert 'long' format 'wide' `spread.

library(dplyr) library(tidyr) df1 %>%     mutate(numberofdays=paste0('lag', numberofdays)) %>%     spread(numberofdays, closing_price)  

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -