Reputation: 464
I want to preallocate space for dates in an R dataframe.
To preallocate space for characters and numbers, I would write
MyDataframe <- data.frame(Name = character(10),
Tickets = numeric(10))
What would I add next to preallocate space for dates? This does not work...
# DOES NOT WORK
MyDataframe <- data.frame(Name = character(10),
Tickets = numeric(10)
Date = Date(10))
Currently, I define the column as numeric, then coerce to dates, but this seems less than ideal.
Thanks
Upvotes: 4
Views: 11121
Reputation: 464
Here is my final code. It works well.
MyDF <- data.frame(Name = character(10),
Tickets = numeric(10),
Date = as.Date(rep(0,10), origin = "1900-01-01"))
Here is the output:
> MyDF
Name Tickets Date
1 0 1900-01-01
2 0 1900-01-01
3 0 1900-01-01
4 0 1900-01-01
5 0 1900-01-01
6 0 1900-01-01
7 0 1900-01-01
8 0 1900-01-01
9 0 1900-01-01
10 0 1900-01-01
And the summary
> summary(MyDF)
Name Tickets Date
:10 Min. :0 Min. :1900-01-01
1st Qu.:0 1st Qu.:1900-01-01
Median :0 Median :1900-01-01
Mean :0 Mean :1900-01-01
3rd Qu.:0 3rd Qu.:1900-01-01
Max. :0 Max. :1900-01-01
Thanks for your help
Upvotes: 1
Reputation: 4659
If all you are looking for is a placeholder column with 10 dates, this works:
as.Date(1:10, origin=Sys.Date())
Your problem is that the character()
and numeric()
functions take a length argument, but
as.Date
(as there is no Date()
function) does not (?as.Date
)
Upvotes: 5