将R中的数据帧行转换为日期

时间:2015-01-05 18:05:51

标签: r date dataframe

是否有一种简单的方法可以将行(而不是列)更改为更大数据框中的日期类?

我想要做的就是让数据框的第一行是与每列中所有数据相关的日期(特别是月末)。但是,当我将日期转换为变通方​​法(首先以列形式转换为日期,然后转置为行并切换到数据框)时,日期会转换回因子(如下所示)。

datetemp$monthdate <- as.Date(datetemp$Month, '%m/%d/%Y')
monthdates <- as.data.frame(t(datetemp))
monthrow <- monthdates[-1, ] #to delete first row and leave a 1x121 df
row.names(monthrow) <- NULL
class(monthrow[1, 2]) #this is factor, not date as desired 

我还尝试将日期列表保留为日期格式(下面的daterow3是Date [121]格式)。但是当我尝试将日期行绑定到稍后的数据帧时,它会给出下面的错误,即使原始列表和日期列表都有121列。

datecol <- datetemp[, -1]
daterow2 <- t(datecol)
daterow3 <- unname(daterow2)
# Error in rbind(deparse.level, ...) : 
# numbers of columns of arguments do not match

如果可能的话,请分享一个明智的解决方法,这样我就可以在df的第一行而不是第一列中显示日期。谢谢你的帮助。

***编辑:根据要求,下面的datetemp对象:

structure(list(Month = structure(c(121L, 1L, 41L, 51L, 61L, 71L, 
81L, 91L, 101L, 111L, 11L, 21L, 31L, 2L, 42L, 52L, 62L, 72L, 
82L, 92L, 102L, 112L, 12L, 22L, 32L, 3L, 43L, 53L, 63L, 73L, 
83L, 93L, 103L, 113L, 13L, 23L, 33L, 4L, 49L, 54L, 64L, 74L, 
84L, 94L, 104L, 114L, 14L, 24L, 34L, 5L, 44L, 55L, 65L, 75L, 
85L, 95L, 105L, 115L, 15L, 25L, 35L, 6L, 45L, 56L, 66L, 76L, 
86L, 96L, 106L, 116L, 16L, 26L, 36L, 7L, 46L, 57L, 67L, 77L, 
87L, 97L, 107L, 117L, 17L, 27L, 37L, 8L, 50L, 58L, 68L, 78L, 
88L, 98L, 108L, 118L, 18L, 28L, 38L, 9L, 47L, 59L, 69L, 79L, 
89L, 99L, 109L, 119L, 19L, 29L, 39L, 10L, 48L, 60L, 70L, 80L, 
90L, 100L, 110L, 120L, 20L, 30L, 40L), .Names = c("CUSIP", "X12.31.2004.1.31.2005", 
"X1.31.2005.2.28.2005", "X2.28.2005.3.31.2005", "X3.31.2005.4.29.2005", 
"X4.29.2005.5.31.2005", "X5.31.2005.6.30.2005", "X6.30.2005.7.29.2005", 
"X7.29.2005.8.31.2005", "X8.31.2005.9.30.2005", "X9.30.2005.10.31.2005", 
"X10.31.2005.11.30.2005", "X11.30.2005.12.30.2005", "X12.30.2005.1.31.2006", 
"X1.31.2006.2.28.2006", "X2.28.2006.3.31.2006", "X3.31.2006.4.28.2006", 
"X4.28.2006.5.31.2006", "X5.31.2006.6.30.2006", "X6.30.2006.7.31.2006", 
"X7.31.2006.8.31.2006", "X8.31.2006.9.29.2006", "X9.29.2006.10.31.2006", 
"X10.31.2006.11.30.2006", "X11.30.2006.12.29.2006", "X12.29.2006.1.31.2007", 
"X1.31.2007.2.28.2007", "X2.28.2007.3.30.2007", "X3.30.2007.4.30.2007", 
"X4.30.2007.5.31.2007", "X5.31.2007.6.29.2007", "X6.29.2007.7.31.2007", 
"X7.31.2007.8.31.2007", "X8.31.2007.9.28.2007", "X9.28.2007.10.31.2007", 
"X10.31.2007.11.30.2007", "X11.30.2007.12.31.2007", "X12.31.2007.1.31.2008", 
"X1.31.2008.2.29.2008", "X2.29.2008.3.31.2008", "X3.31.2008.4.30.2008", 
"X4.30.2008.5.30.2008", "X5.30.2008.6.30.2008", "X6.30.2008.7.31.2008", 
"X7.31.2008.8.29.2008", "X8.29.2008.9.30.2008", "X9.30.2008.10.31.2008", 
"X10.31.2008.11.28.2008", "X11.28.2008.12.31.2008", "X12.31.2008.1.30.2009", 
"X1.30.2009.2.27.2009", "X2.27.2009.3.31.2009", "X3.31.2009.4.30.2009", 
"X4.30.2009.5.29.2009", "X5.29.2009.6.30.2009", "X6.30.2009.7.31.2009", 
"X7.31.2009.8.31.2009", "X8.31.2009.9.30.2009", "X9.30.2009.10.30.2009", 
"X10.30.2009.11.30.2009", "X11.30.2009.12.31.2009", "X12.31.2009.1.29.2010", 
"X1.29.2010.2.26.2010", "X2.26.2010.3.31.2010", "X3.31.2010.4.30.2010", 
"X4.30.2010.5.31.2010", "X5.31.2010.6.30.2010", "X6.30.2010.7.30.2010", 
"X7.30.2010.8.31.2010", "X8.31.2010.9.30.2010", "X9.30.2010.10.29.2010", 
"X10.29.2010.11.30.2010", "X11.30.2010.12.31.2010", "X12.31.2010.1.31.2011", 
"X1.31.2011.2.28.2011", "X2.28.2011.3.31.2011", "X3.31.2011.4.29.2011", 
"X4.29.2011.5.31.2011", "X5.31.2011.6.30.2011", "X6.30.2011.7.29.2011", 
"X7.29.2011.8.31.2011", "X8.31.2011.9.30.2011", "X9.30.2011.10.31.2011", 
"X10.31.2011.11.30.2011", "X11.30.2011.12.30.2011", "X12.30.2011.1.31.2012", 
"X1.31.2012.2.29.2012", "X2.29.2012.3.30.2012", "X3.30.2012.4.30.2012", 
"X4.30.2012.5.31.2012", "X5.31.2012.6.29.2012", "X6.29.2012.7.31.2012", 
"X7.31.2012.8.31.2012", "X8.31.2012.9.28.2012", "X9.28.2012.10.31.2012", 
"X10.31.2012.11.30.2012", "X11.30.2012.12.31.2012", "X12.31.2012.1.31.2013", 
"X1.31.2013.2.28.2013", "X2.28.2013.3.29.2013", "X3.29.2013.4.30.2013", 
"X4.30.2013.5.31.2013", "X5.31.2013.6.28.2013", "X6.28.2013.7.31.2013", 
"X7.31.2013.8.30.2013", "X8.30.2013.9.30.2013", "X9.30.2013.10.31.2013", 
"X10.31.2013.11.29.2013", "X11.29.2013.12.31.2013", "X12.31.2013.1.31.2014", 
"X1.31.2014.2.28.2014", "X2.28.2014.3.31.2014", "X3.31.2014.4.30.2014", 
"X4.30.2014.5.30.2014", "X5.30.2014.6.30.2014", "X6.30.2014.7.31.2014", 
"X7.31.2014.8.29.2014", "X8.29.2014.9.30.2014", "X9.30.2014.10.31.2014", 
"X10.31.2014.11.28.2014", "X11.28.2014.12.31.2014"), .Label = c("1/31/2005", 
"1/31/2006", "1/31/2007", "1/31/2008", "1/31/2009", "1/31/2010", 
"1/31/2011", "1/31/2012", "1/31/2013", "1/31/2014", "10/31/2005", 
"10/31/2006", "10/31/2007", "10/31/2008", "10/31/2009", "10/31/2010", 
"10/31/2011", "10/31/2012", "10/31/2013", "10/31/2014", "11/30/2005", 
"11/30/2006", "11/30/2007", "11/30/2008", "11/30/2009", "11/30/2010", 
"11/30/2011", "11/30/2012", "11/30/2013", "11/30/2014", "12/31/2005", 
"12/31/2006", "12/31/2007", "12/31/2008", "12/31/2009", "12/31/2010", 
"12/31/2011", "12/31/2012", "12/31/2013", "12/31/2014", "2/28/2005", 
"2/28/2006", "2/28/2007", "2/28/2009", "2/28/2010", "2/28/2011", 
"2/28/2013", "2/28/2014", "2/29/2008", "2/29/2012", "3/31/2005", 
"3/31/2006", "3/31/2007", "3/31/2008", "3/31/2009", "3/31/2010", 
"3/31/2011", "3/31/2012", "3/31/2013", "3/31/2014", "4/30/2005", 
"4/30/2006", "4/30/2007", "4/30/2008", "4/30/2009", "4/30/2010", 
"4/30/2011", "4/30/2012", "4/30/2013", "4/30/2014", "5/31/2005", 
"5/31/2006", "5/31/2007", "5/31/2008", "5/31/2009", "5/31/2010", 
"5/31/2011", "5/31/2012", "5/31/2013", "5/31/2014", "6/30/2005", 
"6/30/2006", "6/30/2007", "6/30/2008", "6/30/2009", "6/30/2010", 
"6/30/2011", "6/30/2012", "6/30/2013", "6/30/2014", "7/31/2005", 
"7/31/2006", "7/31/2007", "7/31/2008", "7/31/2009", "7/31/2010", 
"7/31/2011", "7/31/2012", "7/31/2013", "7/31/2014", "8/31/2005", 
"8/31/2006", "8/31/2007", "8/31/2008", "8/31/2009", "8/31/2010", 
"8/31/2011", "8/31/2012", "8/31/2013", "8/31/2014", "9/30/2005", 
"9/30/2006", "9/30/2007", "9/30/2008", "9/30/2009", "9/30/2010", 
"9/30/2011", "9/30/2012", "9/30/2013", "9/30/2014", "Month"), class = "factor"), 
    monthdate = structure(c(NA, 12814, 12842, 12873, 12903, 12934, 
    12964, 12995, 13026, 13056, 13087, 13117, 13148, 13179, 13207, 
    13238, 13268, 13299, 13329, 13360, 13391, 13421, 13452, 13482, 
    13513, 13544, 13572, 13603, 13633, 13664, 13694, 13725, 13756, 
    13786, 13817, 13847, 13878, 13909, 13938, 13969, 13999, 14030, 
    14060, 14091, 14122, 14152, 14183, 14213, 14244, 14275, 14303, 
    14334, 14364, 14395, 14425, 14456, 14487, 14517, 14548, 14578, 
    14609, 14640, 14668, 14699, 14729, 14760, 14790, 14821, 14852, 
    14882, 14913, 14943, 14974, 15005, 15033, 15064, 15094, 15125, 
    15155, 15186, 15217, 15247, 15278, 15308, 15339, 15370, 15399, 
    15430, 15460, 15491, 15521, 15552, 15583, 15613, 15644, 15674, 
    15705, 15736, 15764, 15795, 15825, 15856, 15886, 15917, 15948, 
    15978, 16009, 16039, 16070, 16101, 16129, 16160, 16190, 16221, 
    16251, 16282, 16313, 16343, 16374, 16404, 16435), class = "Date")), .Names = c("Month", 
"monthdate"), row.names = c(NA, -121L), class = "data.frame")

0 个答案:

没有答案