create an empty data frame and then fill in it

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

create an empty data frame and then fill in it

Stefano Sofia
Dear R list users,
sorry for this simple question, but I already spent many efforts to solve it.

I create an empty data frame called df_year like

df_year <- data.frame(day=as.Date(character()), hs_MteBove=integer(), hs_MtePrata=integer(), hs_Pintura=integer(), hs_Pizzo=integer(), hs_Sassotetto=integer(), hs_Sibilla=integer(), stringsAsFactors=FALSE)

and then I start to fill in it with

df_year$day <- seq(as.Date("2004-11-01-00-00","%Y-%m-%d"), as.Date("2005-05-01-00-00","%Y-%m-%d"), by="day")

but I get the following error:
"replacement has 182 rows, data has 0"

Where is my silly mistake?

Thank you for your help
Stefano


________________________________

AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: create an empty data frame and then fill in it

Duncan Murdoch-2
On 09/06/2016 6:22 AM, Stefano Sofia wrote:

> Dear R list users,
> sorry for this simple question, but I already spent many efforts to solve it.
>
> I create an empty data frame called df_year like
>
> df_year <- data.frame(day=as.Date(character()), hs_MteBove=integer(), hs_MtePrata=integer(), hs_Pintura=integer(), hs_Pizzo=integer(), hs_Sassotetto=integer(), hs_Sibilla=integer(), stringsAsFactors=FALSE)
>
> and then I start to fill in it with
>
> df_year$day <- seq(as.Date("2004-11-01-00-00","%Y-%m-%d"), as.Date("2005-05-01-00-00","%Y-%m-%d"), by="day")
>
> but I get the following error:
> "replacement has 182 rows, data has 0"
>
> Where is my silly mistake?

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: create an empty data frame and then fill in it

Duncan Murdoch-2
In reply to this post by Stefano Sofia
On 09/06/2016 6:22 AM, Stefano Sofia wrote:

> Dear R list users,
> sorry for this simple question, but I already spent many efforts to solve it.
>
> I create an empty data frame called df_year like
>
> df_year <- data.frame(day=as.Date(character()), hs_MteBove=integer(), hs_MtePrata=integer(), hs_Pintura=integer(), hs_Pizzo=integer(), hs_Sassotetto=integer(), hs_Sibilla=integer(), stringsAsFactors=FALSE)
>
> and then I start to fill in it with
>
> df_year$day <- seq(as.Date("2004-11-01-00-00","%Y-%m-%d"), as.Date("2005-05-01-00-00","%Y-%m-%d"), by="day")
>
> but I get the following error:
> "replacement has 182 rows, data has 0"
>
> Where is my silly mistake?

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.