Create an empty data.frame

前端 未结 17 1078
猫巷女王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:26

    You could use read.table with an empty string for the input text as follows:

    colClasses = c("Date", "character", "character")
    col.names = c("Date", "File", "User")
    
    df <- read.table(text = "",
                     colClasses = colClasses,
                     col.names = col.names)
    

    Alternatively specifying the col.names as a string:

    df <- read.csv(text="Date,File,User", colClasses = colClasses)
    

    Thanks to Richard Scriven for the improvement

提交回复
热议问题