Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing the display format a R data frame

currently working with below lines:

library(quantmod)
library(pedquant)



option_chain<-function(ticker,exp){
  view_window <- function(dfr, var, x, pm){
    
    idx <- which(abs(dfr[, var]-x) == min(abs(dfr[, var] - x)))
    
    return(dfr[(idx - pm) : (idx + pm), ])
    
  }
  options<-getOptionChain(ticker,exp)
  call_filter<-options$calls[,c("Strike","Last","Vol")]

  put_filter<-options$puts[,c("Strike","Last","Vol")]

  ticker_price<-md_stock(ticker,type="real")
  price<-ticker_price$close
  call<-view_window(dfr=call_filter,
                    var="Strike",
                    x=price,
                    pm=5)
  
  put<-view_window(dfr=put_filter,
                   var="Strike",
                   x=price,
                   pm=5)
  
  colnames(call)<-c('Option Strike',"Call Price","Call Volume")
  colnames(put)<-c('Option Strike',"Put Price","Put Volume")
  
  
  print(ticker_price$symbol)
  print(ticker_price$time)
  print(price)
  print(call)
  print(put)
}

ticker<-"BABA"
exp="2023-06-30"
option_chain(ticker,exp)

 Option Strike Call Price Call Volume
18            81       5.40           3
19            82       4.56          13
20            83       3.47         131
21            84       2.81         251
22            85       2.16        1243
23            86       1.67        4837
24            87       1.23        3685
25            88       0.90        3885
26            89       0.63        4531
27            90       0.47       13354
28            91       0.33        1061
   Option Strike Put Price Put Volume
25            81      0.16        412
26            82      0.30        996
27            83      0.45       1353
28            84      0.71        723
29            85      1.06        809
30            86      1.60        806
31            87      2.18        142
32            88      2.83         58
33            89      3.60        119
34            90      4.65         29
35            91      5.35          6

Would like to ask, if I would like to change the format to the one like the attached picture, is there a way that I can do so? Million thanks.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All I would like to do, is to re-arrange the column and delete one column, that is, the Option Strike column.

>Solution :

You could use select at the end of the code

select(`Call Volume`, `Call Price`, Strike, `Put Price`, `Put Volume`) 

Illegal column names require back tick (column names with spaces)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading