I\'m trying to initialize a data.frame without any rows. Basically, I want to specify the data types for each column and name them, but not have any rows created as a result
This question didn't specifically address my concerns (outlined here) but in case anyone wants to do this with a parameterized number of columns and no coercion:
> require(dplyr)
> dbNames <- c('a','b','c','d')
> emptyTableOut <-
data.frame(
character(),
matrix(integer(), ncol = 3, nrow = 0), stringsAsFactors = FALSE
) %>%
setNames(nm = c(dbNames))
> glimpse(emptyTableOut)
Observations: 0
Variables: 4
$ a <chr>
$ b <int>
$ c <int>
$ d <int>
As divibisan states on the linked question,
...the reason [coercion] occurs [when cbinding matrices and their constituent types] is that a matrix can only have a single data type. When you cbind 2 matrices, the result is still a matrix and so the variables are all coerced into a single type before converting to a data.frame
If you already have an existent data frame, let's say df
that has the columns you want, then you can just create an empty data frame by removing all the rows:
empty_df = df[FALSE,]
Notice that df
still contains the data, but empty_df
doesn't.
I found this question looking for how to create a new instance with empty rows, so I think it might be helpful for some people.
You can do it without specifying column types
df = data.frame(matrix(vector(), 0, 3,
dimnames=list(c(), c("Date", "File", "User"))),
stringsAsFactors=F)
I created empty data frame using following code
df = data.frame(id = numeric(0), jobs = numeric(0));
and tried to bind some rows to populate the same as follows.
newrow = c(3, 4)
df <- rbind(df, newrow)
but it started giving incorrect column names as follows
X3 X4
1 3 4
Solution to this is to convert newrow to type df as follows
newrow = data.frame(id=3, jobs=4)
df <- rbind(df, newrow)
now gives correct data frame when displayed with column names as follows
id nobs
1 3 4
Say your column names are dynamic, you can create an empty row-named matrix and transform it to a data frame.
nms <- sample(LETTERS,sample(1:10))
as.data.frame(t(matrix(nrow=length(nms),ncol=0,dimnames=list(nms))))