[英]What is the best way to turn a data set with first column headers and the data being in rows in R? How do I convert them to columns?
[英]With downloaded Bloomberg data, how do you convert current column headers to rows in R?
我已经从彭博社下载了债券(ISIN)的原始数据,每周日期及其信用息差。 麻烦的是,ISIN位于列标题中,而日期是行。 本着整洁的数据精神,我试图将ISIN转换为R中的行。有人可以提出建议吗?
发布可重现的示例或至少一些示例数据将很有帮助,但此处附带一些虚拟数据。 我使用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
那是您要找的东西吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.