1

I'm sorry for bothering with this but can't find solution to this.

I have a data.frame with column name date:

 str(df$date)
 Factor w/ 360 levels "1982-11-30","1982-12-31",..: 1 4 7 10 13 16 19 22 25 28 ...

 class(a)
 [1] "factor"

I would like to convert this to numeric values: from: "1982-11-30" to 19821130 or else.

EDIT:

Initially I have had this in numeric format and converted to factor as following:

date <- as.Date(as.character(df$date_num),format="%Y%m%d")

So how to reverse this?

4

1 回答 1

2

像这样的东西?

dd <- structure(1:2, .Label = c("2013-01-01", "2013-02-01"), class = "factor")
# [1] 2013-01-01 2013-02-01
# Levels: 2013-01-01 2013-02-01

as.numeric(gsub("-", "", as.character(dd), fixed=TRUE))
# [1] 20130101 20130201
于 2013-08-10T09:56:49.417 回答