Create an empty data.frame

前端 未结 17 937
猫巷女王i
猫巷女王i 2020-11-22 16:06

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

相关标签:
17条回答
  • 2020-11-22 16:12

    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'))
    
    0 讨论(0)
  • 2020-11-22 16:14

    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()
    
    0 讨论(0)
  • 2020-11-22 16:16

    Just declare

    table = data.frame()
    

    when you try to rbind the first line it will create the columns

    0 讨论(0)
  • 2020-11-22 16:17

    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"
    
    0 讨论(0)
  • 2020-11-22 16:19

    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.

    0 讨论(0)
  • 2020-11-22 16:21

    By Using data.table we can specify data types for each column.

    library(data.table)    
    data=data.table(a=numeric(), b=numeric(), c=numeric())
    
    0 讨论(0)
提交回复
热议问题