0

Trying to sort a dataframe with time series of stock betas. Would like to create two portfolios, one with stocks that have higher than median beta, and another with lower than median beta for each month. I need to do this row-wise, as each row is a new month.

This is what i have tried so far. not working... I've removed Names which in our dataset is the variable containing dates. Then i have converted characters to NA, and lastly trying to get a new column with median values for each row.

Have not started sorting yet, no idea where to begin here.

Thanks for any help.

My data looks like this:

Screenshot of data

OMXCB_END_MED <- OMXCB_END %>%
 select(-Names) %>%
 mutate(across(where(is.character), ~na_if(.,"N/A"))) %>%
 rowwise() %>%
 mutate(median = median(across(cols = everything()), na.rm = TRUE))
Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • 1
    Welcome to StackOverflow! Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help us help you. A screenshot of the data is pretty hard for us to work on. It would be awesome if you can also share the desired output format/example. – Adam Quek Jun 07 '22 at 08:19
  • 1
    use `median(c_across(cur_data()), na.rm = TRUE)` This is a messy data. Consider pivoting it longer for it to be tidy and easy to work with – Onyambu Jun 07 '22 at 08:19
  • Thank you! Yes, the data was really messy, so have gone back to the drawing board on this one. – Jinoor Sellathurai Jun 08 '22 at 11:00

0 Answers0