![](/img/trans.png)
[英]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.