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?
Asked
Active
Viewed 46 times
1 Answers
0
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?

Peter Smittenaar
- 321
- 2
- 9
-
Not quite. In my limited experience with data wrangling in R, the key variables tend to be column headers. Right now, here's a sample of my data set - with ISIN numbers located as column headers. Date XS1785422731 XS1856094724 XS0872777122 02/01/2015 #N/A N/A #N/A N/A 1705.669 09/01/2015 #N/A N/A #N/A N/A 2096.65 My question lies with how do I transpose these column headers into rows in R? – Desmond May 13 '19 at 05:00
-
@Desmond I'm happy to help further but please update your questions with some of the actual data you're working with - see [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) specifically about `dput()`. Also, the key variables can be in column headers but `melt`ing a dataframe brings it from "wide" to "long" format, which is the norm for timeseries data (and indeed for other functions in R such as `ggplot`) – Peter Smittenaar May 13 '19 at 14:15