KrOstir
KrOstir

Reputation: 94

Create a data frame with date as column names

I would like to construct a data table in R that has columns as dates and rows as times (without date info). Basically I have a table in the form:

Time  21.04.15 22.04.15 24.04.15 03.05.15
00:00      0.4      0.4      0.4      0.4
01:00      0.4      0.4      0.4      0.4
02:00      0.4      0.4      0.4      0.4
03:00      0.6      0.6      0.6      0.6
04:00      0.6      0.6      0.6      0.6
05:00      0.7      0.8      0.8      0.8
06:00      0.7      0.8      0.8      0.8
07:00      0.7      0.8      0.8      0.8
...

I would like to address (plot, extract) the columns by date and elements by date and time.

Is this possible?

Upvotes: 2

Views: 11771

Answers (1)

Benjamin
Benjamin

Reputation: 17279

The best you can do is rename them character strings that represent dates, but I don't think the names themselves can be a Date object. (I'll admit, I've never tried, and I'm not going to experiment with it because doing so seems like a really bad idea).

Assuming your current column names are in dd.mm.yy format, run

names(df_object) <- format(as.Date(names(df_object), format = "%d.%m.%y"),
    format = "%Y-%m-%d")

But like those in the comments, while this will work, I have a hard time imagining circumstances where it is beneficial.

Upvotes: 2

Related Questions