[R] Ordering the rows of a data frame

Ista Zahn istazahn at gmail.com
Fri Jan 3 16:05:13 CET 2014


On Fri, Jan 3, 2014 at 9:41 AM, Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
>
> Dear R users,
> I have two files of seasonal rainfall data (more than 10,000 rows each); here the first 8 rows of each file are reported.
>
> Code_Raingouge,Y_init,M_init,D_init,h_init,m_init,Y_fin,M_fin,D_fin,h_fin,m_fin,Rainfall,N_Values,Quality_Level,Code_Station
> 2000,1952,12,1,0,0,1953,3,1,0,0,307.20,90,100.0,1510
> 2000,1953,3,1,0,0,1953,6,1,0,0,153.60,92,100.0,1510
> 2000,1953,6,1,0,0,1953,9,1,0,0,181.00,92,100.0,1510
> 2000,1953,9,1,0,0,1953,12,1,0,0,202.40,91,100.0,1510
> 2000,1953,12,1,0,0,1954,3,1,0,0,153.80,90,100.0,1510
> 2000,1954,3,1,0,0,1954,6,1,0,0,286.20,92,100.0,1510
> 2000,1954,6,1,0,0,1954,9,1,0,0,142.80,92,100.0,1510
> 2000,1954,9,1,0,0,1954,12,1,0,0,186.60,91,100.0,1510
> ...
>
> Code_Raingouge,Y_init,M_init,D_init,h_init,m_init,Y_fin,M_fin,D_fin,h_fin,m_fin,Rainfall,N_Values,Quality_Level,Code_Station
> 1056,2004,12,1,0,0,2005,3,1,0,0,93.60,2833,32.8,15
> 1056,2005,3,1,0,0,2005,6,1,0,0,149.80,4406,49.9,15
> 1056,2005,6,1,0,0,2005,9,1,0,0,52.80,1440,16.3,15
> 1056,2005,9,1,0,0,2005,12,1,0,0,191.80,1201,13.7,15
> 1056,2005,12,1,0,0,2006,3,1,0,0,26.40,336,3.9,15
> 1056,2006,12,1,0,0,2007,3,1,0,0,59.00,3604,41.7,15
> 1056,2007,3,1,0,0,2007,6,1,0,0,181.16,4414,50.0,15
> 1056,2007,6,1,0,0,2007,9,1,0,0,96.00,7337,83.1,15
> ...
>
> I have to load them as data frames, to merge them and sort them by (Y_init,M_init,D_init,Code_Raingouge).
>
> I wrote a short function where I first load the two files as data frames
>
> df_1 <- read.csv(file="prec_all_19521201_19821201.csv", sep=",")
> df_2 <- read.csv(file="prec_all_19821201_20111201.csv", sep=",")
>
> then I merge them by
>
> df_final <- merge(df_1, df_2, all=TRUE)
>
> and finally I try to order df_final through
>
> df_final <- df_final[order(Y_init, M_init, D_init, Code_Raingouge), ]

You probably need

df_final <- df_final[order(df_final$Y_init, df_final$M_init,
df_final$D_init, df_final$Code_Raingouge), ]

or

df_final <- df_final[with(df_final, order(Y_init, M_init, D_init,
Code_Raingouge)), ]

>
> but this returns only the first row.

That is strange; do you have copies of Y_init, M_init etc. in your workspace?

>
> I read the manual carefully and I saw some examples, but I have not been able to do this simple task.

I can see how the data.frame sorting examples in  ?order would lead to
this confusion, as they are limited to the special case where the
variables you want to sort by are in the workspace rather than (as is
more often the case) variables in the data.frame itself.

> I spent quite a long time and now I decided to ask the list.
> Could please somebody help me to show me where the mistake is?
>
> Thank you
> 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]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>




More information about the R-help mailing list