在Date列标题上使用melt()

时间:2019-03-16 13:37:32

标签: r

这是我的excel如下所示的样子。我想使用这些日期(x4中的列名称为“ cols”)来绘制图形。

enter image description here

structure(list(Date = c("DBS SP Equity", "ST SP Equity", "OCBC SP Equity", 
"UOB SP Equity", "WIL SP Equity"), Industry = c("Banks", "Telecommunications", 
"Banks", "Banks", "Food"), `43402` = c(-0.00476190476190486, 
0.00958466453674123, 0.000961538461538458, 0.00332363938512659, 
0), `43399` = c(-0.0257275411218895, -0.00634920634920633, -0.0188679245283018, 
-0.0352705410821643, 0.00643086816720251), `43398` = c(-0.0161825726141079, 
0.00318471337579607, -0.00375939849624074, -0.00279776179056757, 
0.0130293159609121), `43397` = c(0.00542344597413447, 0.00319488817891389, 
0.00757575757575757, 0.00481927710843388, 0.00655737704918025
), `43396` = c(-0.0156057494866531, -0.0126182965299685, -0.0231267345050878, 
-0.015810276679842, 0)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

data4 <- read_excel("data.xlsx", sheet = 4, col_names = TRUE)
    x4 <- melt(data4, id = c("Industry"), measure.vars = 3:997,  variable = "cols")
    print(x4$cols)

打印时该值为整数No。
我已经尝试过了,但它的显示不适用Cols

x4$cols <- as.Date(x4$cols, format = "%yyyy-%mm-%dd")

我也尝试了以下代码。再次显示int。

x4 <- x4 %>% mutate(Date = dmy(cols))

如何获取实际日期?

1 个答案:

答案 0 :(得分:1)

实际日期是从1970-01-01开始的整数偏移量(起点)。您可能会用void之类的东西转换回来。另请参阅:https://stats.idre.ucla.edu/r/faq/how-does-r-handle-date-values/