r - Unstacking huge dataset with stock returns -


i have long crsp data.frame , trying unstack wide format.

the dataframe contains kypermno (stock identifier), caldat (date yyyy-mm-dd), prc (price), adjprc (adjusted price) , ret (return). dataframe sorted stock (kypermno) , date (caldat), each row stands 1 stock @ given date.

enter image description here

the format want is: each kypermno column(name) , each day (date) row(name) , returns in corresponding fields (dropping prc , adjprc).

i tried this: d_ret <- as.data.frame(unstack(hilfs_ret, ret ~ kypermno))

the problem not stocks have return on days, error message:

"error in (function (..., row.names = null, check.rows = false, check.names = true, : argumente implizieren unterschiedliche anzahl zeilen" (in english: arguments imply different number of rows)

if stock not have return or did not exist @ date, there should "na" filling.

this error message not appear when skip "as.data.frame" function, want have data.frame , not list.

solutions loop , if statement not feasible since dataset large (48 million entries).

is there anyway solve this? maybe reshape function?

thank , have nice day!

kind regards

kb

you use functions tidyverse collection of packages. let's data frame called "long_df." code should convert data frame named "wide_df."

library(tidyverse) wide_df <- tidyr::spread(long_df %>% select(-prc, -adjprc),                           key= kypermno,                          value = ret) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -