user4605941
user4605941

Reputation: 185

Initializing a dataframe to contain `Date` and `decimal`

I want to initialize a dataframe that contains as rows: Date, integer, integer, and decimal.

Here is what I have so far:

df = data.frame(Date = date(), a = integer(0), b = integer(0), ratio = numeric(0.0), stringsAsFactors = FALSE)

This gives me the error:

Error in data.frame(Date = date(), a = integer(0), b = integer(0), ratio = numeric(0),  : 

arguments imply differing number of rows: 1, 0

When I remove the Date = date() part, I no longer have this error.

I also tried:

df = data.frame(Date = date(0), a = integer(0), b = integer(0), ratio = numeric(0.0), stringsAsFactors = FALSE)

This gave me the error:

Error in date(0) : unused argument (0)

I have two questions:

  1. How do I initialize a Date in a dataframe?
  2. How do I initialize a decimal in a dataframe? Is doing numeric(0.0) how you do it?

Upvotes: 0

Views: 624

Answers (1)

akrun
akrun

Reputation: 887531

Try

df <- data.frame(Date = as.Date(character()), a = integer(), 
     b = integer(), ratio = numeric(), stringsAsFactors = FALSE)
str(df)
#'data.frame':  0 obs. of  4 variables:
#$ Date :Class 'Date'  num(0) 
#$ a    : int 
#$ b    : int 
#$ ratio: num 

Upvotes: 1

Related Questions