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
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
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))
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.
Would a dataframe of NA
s 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
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)