I have a "wide" dataframe that has dates as column headings. Using the transform function, I can turn it into a "long" dataframe. I need the dates however (column headings) to be actual data points as a new variable. Let me illustrate the problem with a similar dataframe:
matrix <- matrix(round(runif(25)*10), nrow = 5, ncol = 5)
hospital_names <- c("Hosp A", "Hosp B", "Hosp C", "Hosp D", "Hosp E")
dates <- c("Jan 03", "Feb 03", "Mar 03", "Apr 03", "May 03")
df <- as.data.frame(matrix)
colnames(df) = dates
df <- cbind(hospital_names, df)
This gives a dataframe that looks like this:
hospital_names Jan 03 Feb 03 Mar 03 Apr 03 May 03
1 Hosp A 10 3 1 9 2
2 Hosp B 8 5 8 6 0
3 Hosp C 9 1 9 2 5
4 Hosp D 4 0 7 0 4
5 Hosp E 0 8 9 2 4
I now apply the transform function:
df <- t(df)
And get this:
[,1] [,2] [,3] [,4] [,5]
hospital_names "Hosp A" "Hosp B" "Hosp C" "Hosp D" "Hosp E"
Jan 03 "10" "8" "9" "4" "0"
Feb 03 "3" "5" "1" "0" "8"
Mar 03 "1" "8" "9" "7" "9"
Apr 03 "9" "6" "2" "0" "2"
May 03 "2" "0" "5" "4" "4"
So at this point, my dates are actual row names instead of data points. My question is, is there an easy fix? Can I convert the row names into a vector and then use cbind to add it as a variable?