[R] Howto crosstable-ing......

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 22 17:55:04 CEST 2005


On Wed, 2005-06-22 at 17:12 +0200, v.demartino2 at virgilio.it wrote:
> I receive the following meteo dataset regularly, containing the average
> daily temperatures (tMedia) of a certain month for 24 selected meteo-stations
> (COD_WMO) whose human-readable names are in (NOME).
> 
> str(tabella)
> `data.frame':	1038 obs. of  4 variables:
>  $ COD_WMO: int  16045 16045 16045 16045 16045 16045 16045 16045 16045 16045
> ...
>  $ NOME   : Factor w/ 24 levels "ALGHERO","BARI/PALESE MACCHIE",..: 22 22
> 22 22 22 22 22 22 22 22 ...
>  $ DATE   :'POSIXct', format: chr  "2005-05-01" "2005-05-02" "2005-05-03"
> "2005-05-04" ...
>  $ tMedia : num  11.7 18.6 16.9 19.7 15.0 ...
> 
> 
> Here you are a short list of it:
>     COD_WMO      NOME       DATE   tMedia
> 505   16191 FALCONARA 2005-06-01  20.95
> 506   16191 FALCONARA 2005-06-02  20.15
> 507   16191 FALCONARA 2005-06-03  18.60
> 506   16191 FALCONARA 2005-06-02  20.15
> 507   16191 FALCONARA 2005-06-03  18.60
> 508   16191 FALCONARA 2005-06-04  22.30
> 509   16191 FALCONARA 2005-06-05     NA
> 510   16191 FALCONARA 2005-06-06     NA
> 511   16191 FALCONARA 2005-06-07  18.20
> 549   16206 GROSSETO 2005-06-01  20.65
> 550   16206 GROSSETO 2005-06-02  21.95
> 551   16206 GROSSETO 2005-06-03  22.25
> 552   16206 GROSSETO 2005-06-04  20.15
> 553   16206 GROSSETO 2005-06-05     NA
> 554   16206 GROSSETO 2005-06-06     NA
> 555   16206 GROSSETO 2005-06-07  22.35
> .....................................................................
> .....................................................................
> 
> 
> I need to rearrange tMedia into a new dataframe whose column names are COD_WMO
>  (or   NOME) and the row is DATE.
> 
> ex.
> DATE            ALGHERO   BARI/PALESE ....  FALCONARA  GROSSETO ..........
> 2005-06-01       16.3             12.8                   17.3          
>  14.0       ...........
> 2005-06-02       18.2               8.9                    18.0        
>   17.9       ..........
> ...............................................................................................
> 
> 
> I read some pieces of R-docs in the internet and run the MASS chapter 2
> examples but without finding anything suitable to my purpose.
> 
> Could you please help me?
> 
> Ciao
> 
> Vittorio

I believe that the following will get you there, based upon your example
output above:

> reshape(tabella[, -1], idvar = "DATE", timevar = "NOME", 
          v.names = "tMedia", direction = "wide")
        DATE tMedia.FALCONARA tMedia.GROSSETO
1 2005-06-01            20.95           20.65
2 2005-06-02            20.15           21.95
3 2005-06-03            18.60           22.25
6 2005-06-04            22.30           20.15
7 2005-06-05               NA              NA
8 2005-06-06               NA              NA
9 2005-06-07            18.20           22.35

See ?reshape for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list