Search code examples
rdataframe

Define dimensions of an empty dataframe


I am trying to collect some data from multiple subsets of a data set and need to create a data frame to collect the results. My problem is don't know how to create an empty data frame with defined number of columns without actually having data to put into it.

collect1 <- c()  ## i'd like to create empty df w/ 3 columns: `id`, `max1` and `min1`

for(i in 1:10){
collect1$id <- i
ss1 <- subset(df1, df1$id == i)
collect1$max1 <- max(ss1$value)
collect1$min1 <- min(ss1$value)
}

I feel very dumb asking this question (I almost feel like I've asked it on SO before but can't find it) but would greatly appreciate any help.


Solution

  • Just create a data frame of empty vectors:

    collect1 <- data.frame(id = character(0), max1 = numeric(0), max2 = numeric(0))
    

    But if you know how many rows you're going to have in advance, you should just create the data frame with that many rows to start with.