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
If you already have a dataframe, you can extract the metadata (column names and types) from a dataframe (e.g. if you are controlling a BUG which is only triggered with certain inputs and need a empty dummy Dataframe):
colums_and_types <- sapply(df, class)
# prints: "c('col1', 'col2')"
print(dput(as.character(names(colums_and_types))))
# prints: "c('integer', 'factor')"
dput(as.character(as.vector(colums_and_types)))
And then use the read.table
to create the empty dataframe
read.table(text = "",
colClasses = c('integer', 'factor'),
col.names = c('col1', 'col2'))
If you want to create an empty data.frame with dynamic names (colnames in a variable), this can help:
names <- c("v","u","w")
df <- data.frame()
for (k in names) df[[k]]<-as.numeric()
You can change the types as well if you need so. like:
names <- c("u", "v")
df <- data.frame()
df[[names[1]]] <- as.numeric()
df[[names[2]]] <- as.character()
Just declare
table = data.frame()
when you try to rbind
the first line it will create the columns
To create an empty data frame, pass in the number of rows and columns needed into the following function:
create_empty_table <- function(num_rows, num_cols) {
frame <- data.frame(matrix(NA, nrow = num_rows, ncol = num_cols))
return(frame)
}
To create an empty frame while specifying the class of each column, simply pass a vector of the desired data types into the following function:
create_empty_table <- function(num_rows, num_cols, type_vec) {
frame <- data.frame(matrix(NA, nrow = num_rows, ncol = num_cols))
for(i in 1:ncol(frame)) {
print(type_vec[i])
if(type_vec[i] == 'numeric') {frame[,i] <- as.numeric(frame[,i])}
if(type_vec[i] == 'character') {frame[,i] <- as.character(frame[,i])}
if(type_vec[i] == 'logical') {frame[,i] <- as.logical(frame[,i])}
if(type_vec[i] == 'factor') {frame[,i] <- as.factor(frame[,i])}
}
return(frame)
}
Use as follows:
df <- create_empty_table(3, 3, c('character','logical','numeric'))
Which gives:
X1 X2 X3
1 <NA> NA NA
2 <NA> NA NA
3 <NA> NA NA
To confirm your choices, run the following:
lapply(df, class)
#output
$X1
[1] "character"
$X2
[1] "logical"
$X3
[1] "numeric"
If you are looking for shortness :
read.csv(text="col1,col2")
so you don't need to specify the column names separately. You get the default column type logical until you fill the data frame.
By Using data.table
we can specify data types for each column.
library(data.table)
data=data.table(a=numeric(), b=numeric(), c=numeric())