R define dimensions of empty data frame

前端 未结 11 1137
清歌不尽
清歌不尽 2020-12-12 21:34

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 empt

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

    It might help the solution given in another forum, Basically is: i.e.

    Cols <- paste("A", 1:5, sep="")
    DF <- read.table(textConnection(""), col.names = Cols,colClasses = "character")
    
    > str(DF)
    'data.frame':   0 obs. of  5 variables:
    $ A1: chr
    $ A2: chr
    $ A3: chr
    $ A4: chr
    $ A5: chr
    

    You can change the colClasses to fit your needs.

    Original link is https://stat.ethz.ch/pipermail/r-help/2008-August/169966.html

    0 讨论(0)
  • 2020-12-12 22:25

    seq_along may help to find out how many rows in your data file and create a data.frame with the desired number of rows

        listdf <- data.frame(ID=seq_along(df),
                                  var1=seq_along(df), var2=seq_along(df))
    
    0 讨论(0)
  • 2020-12-12 22:29

    You can do something like:

    N <- 10
    collect1 <- data.frame(id   = integer(N),
                           max1 = numeric(N),
                           min1 = numeric(N))
    

    Now be careful that in the rest of your code, you forgot to use the row index for filling the data.frame row by row. It should be:

    for(i in seq_len(N)){
       collect1$id[i] <- i
       ss1 <- subset(df1, df1$id == i)
       collect1$max1[i] <- max(ss1$value)
       collect1$min1[i] <- min(ss1$value)
    }
    

    Finally, I would say that there are many alternatives for doing what you are trying to accomplish, some would be much more efficient and use a lot less typing. You could for example look at the aggregate function, or ddply from the plyr package.

    0 讨论(0)
  • 2020-12-12 22:34

    Would a dataframe of NAs work? something like:

    data.frame(matrix(NA, nrow = 2, ncol = 3))

    if you need to be more specific about the data type then may prefer: NA_integer_, NA_real_, NA_complex_, or NA_character_ instead of just NA which is logical

    Something else that may be more specific that the NAs is:

    data.frame(matrix(vector(mode = 'numeric',length = 6), nrow = 2, ncol = 3))

    where the mode can be of any type. See ?vector

    0 讨论(0)
  • 2020-12-12 22:39

    A more general method to create an arbitrary size data frame is to create a n-by-1 data-frame from a matrix of the same dimension. Then, you can immediately drop the first row:

    > v <- data.frame(matrix(NA, nrow=1, ncol=10))
    > v <- v[-1, , drop=FALSE]
    > v
     [1] X1  X2  X3  X4  X5  X6  X7  X8  X9  X10
    <0 rows> (or 0-length row.names)
    
    0 讨论(0)
提交回复
热议问题