我有一个DS,其中包含数字作为列名,如何将它们转换为日期,如下所示。
I/P:
Name 41275 41306 41334 41365 .. 41395
NY 20 33 55 24 .. 656
CA 943 348 44 45 .. 549
O/P:
Name 1/1/2013 2/1/2013 3/1/2013 4/1/2013 .. 5/1/2013
NY 20 33 55 24 .. 656
CA 943 348 44 45 .. 549
提前致谢
答案 0 :(得分:1)
我们可以将as.Date
与origin
names(df1)[-1] <- format(as.Date(as.numeric(names(df1)[-1]),
origin = "1899-12-30"), "%m/%d/%Y")
df1 <- structure(list(Name = structure(c(2L, 1L), .Label = c("CA", "NY"
), class = "factor"), `41275` = c(20L, 943L), `41306` = c(33L,
348L), `41334` = c(55L, 44L), `41365` = c(24L, 45L), `41395` = c(656L,
549L)), .Names = c("Name", "41275", "41306", "41334", "41365",
"41395"), class = "data.frame", row.names = c(NA, -2L))