4

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

2
  • Your second code snippet does not work because you are missing a comma. Even then it would still not work because you are calling a function that does not exist, Date(). Finally if you mention something not working, its always helpful to give the error message you actually see. See here for tips on how to make a reproducible R question.
    – Andy
    Aug 10, 2012 at 16:24
  • Just had the same question - and went round the same loops trying to find a fun that would pre-allocate for dates. Useful solution. I am in general not in favour of filling cells with fake data though. However it looks like there is no way around it in this case. Feb 1, 2013 at 10:09

2 Answers 2

5

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)

0
1

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

1
  • 1
    I would add the argument stringsAsFactors=FALSE to data.frame(). Otherwise if your default.stringsAsFactors=TRUE then character variables will be coerced to Factors with one level and attempts to add values will yield NAs.
    – MattBagg
    May 19, 2014 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.