简体   繁体   English

使用下载的彭博数据,如何将当前列标题转换为R中的行?

[英]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. 我已经从彭博社下载了债券(ISIN)的原始数据,每周日期及其信用息差。 Trouble is, the ISINs are in the column headers, and dates are rows. 麻烦的是,ISIN位于列标题中,而日期是行。 In the spirit of tidy data, I was trying to convert ISINs to rows in R. Could anyone please advise? 本着整洁的数据精神,我试图将ISIN转换为R中的行。有人可以提出建议吗?

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): 我使用reshape2::melt使它整洁(在这种情况下,转换为“长”格式):

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? 那是您要找的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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