简体   繁体   中英

With downloaded Bloomberg data, how do you convert current column headers to rows in R?

I've downloaded raw data of Bonds (ISINs), weekly dates, and their credit spreads from Bloomberg. Trouble is, the ISINs are in the column headers, and dates are rows. In the spirit of tidy data, I was trying to convert ISINs to rows in R. Could anyone please advise?

It would be helpful to post a reproducible example or at least some sample data, but here goes with some dummy data. I use reshape2::melt to make it tidy (into "long" format in this case):

df=data.frame(
    datestamp = c("1999-07-21", "1999-06-08", "1999-07-15", "1999-11-05",
                  "1999-01-29"),
    GOOG = c(3, 4, 5, 6, 7),
    FACEBOOK = c(8, 9, 4, 3, 2)
)
df.long = reshape2::melt(df, id.vars='datestamp')  # anything that is not an id.var gets put into the variable column
print(df.long)

    datestamp variable value
1  1999-07-21     GOOG     3
2  1999-06-08     GOOG     4
3  1999-07-15     GOOG     5
4  1999-11-05     GOOG     6
5  1999-01-29     GOOG     7
6  1999-07-21 FACEBOOK     8
7  1999-06-08 FACEBOOK     9
8  1999-07-15 FACEBOOK     4
9  1999-11-05 FACEBOOK     3
10 1999-01-29 FACEBOOK     2

Is that what you were looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM