I would like to create a dataframe with a single line with NULL values for the columns "Date", "p.value" and "User", but I'm not suceeding with the code below:
df <- data.frame(Date=NULL, "p.value"=NULL, User=NULL)
Does anyone know how to solve it?
CodePudding user response:
For an empty dataframe, you can initialize a vector with the class (e.g., character
). If you are needing a date format, then you can wrap as.Date
around character
.
df <- data.frame(Date=as.Date(character()),
"p.value"=character(),
User=character(),
stringsAsFactors=FALSE)
#[1] Date p.value User
#<0 rows> (or 0-length row.names)
Or you could use NA
if you need an actual row. But this defaults to logical for class. So, if you need columns to be a particular class, then you would still need to wrap the class around the NA
(e.g., as.character(NA)
).
df <- data.frame(Date=NA, "p.value"=NA, User=NA,
stringsAsFactors=FALSE)
# Date p.value User
#1 NA NA NA
You can use NULL
as character, but I don't think it's possible to have only a NULL
row.
CodePudding user response:
This seems to work too:
example_df <- data.frame( "date" = character(0), "p.value" = integer(0), "USER" = integer(0))
Comes up with this:
[1] date p.value USER
<0 rows> (or 0-length row.names)