Skip to content
Prev 361594 / 398506 Next

create an empty data frame and then fill in it

On 09/06/2016 6:22 AM, Stefano Sofia wrote:
Your dataframe has 0 rows, so you can't put a 182 row vector into the 
first column.

Unlike vectors, dataframes won't grow if you make assignments beyond the 
end of the rows.

There are at least a couple of solutions:

1.  Don't create columns until you have data ready for them.

You can wait to create the dataframe until your "day" column is ready:

df_year <- data.frame(day = seq(...))

As you compute other columns of the same length, you can add them, e.g.

df_year$hs_MteBove <- ...

2.  Create your columns with the right length from the beginning:

df_year <- data.frame(day = rep(as.Date(NA), 182), ...)

I don't like this solution as much.

Duncan Murdoch