Dear R-users Sorry for posting the previous message too soon before I have finished it. I have matrix (mprecip) with headers: > dim(mprecip) [1] 6268 170 > mprecip date GilzeRijen Eindhoven Volkel ZuidLimburg Arcen Ubachsberg 1 01/01/1978 NA 0.0 NA 0.1 NA NA 2 01/02/1978 NA 0.0 NA 0.0 NA NA 3 01/03/1978 NA 1.9 NA 0.7 NA NA 4 01/04/1978 NA 3.5 NA 6.9 NA 6.0 5 01/05/1978 NA 1.6 NA 1.8 NA 1.3 6 01/06/1978 NA 0.0 NA 0.0 NA NA 7 01/07/1978 NA 0.0 NA 0.0 NA NA 8 01/08/1978 NA 0.0 NA 0.0 NA NA Columns are: DATE and PRECIP values for each station listed in the header. I would like to transform the matrix into three columns (database like) to be able to load the data in the database. Here is the output I would like to get get it, number of rows = ncol x nrow. Output should look like this: STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP . . . . STATION_NAME2 DATE PRECIP STATION_NAME2 DATE PRECIP STATION_NAME2 DATE PRECIP . . . STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP Thanks again for your help. Rado -- Dr. Radoslav Bonk European Commission - DG Joint Research Centre (JRC) Institute of Environment and Sustainability (IES) LM Unit - Natural Hazards Via E. Fermi, TP 261, 210 20 Ispra (VA), ITALY tel: 0039 0332 78 6013 fax: 0039 0332 78 6653 http://natural-hazards.jrc.it/floods
On Thu, 18 May 2006, Rado Bonk wrote:> Dear R-users > > Sorry for posting the previous message too soon before I have finished it.Hi Rado, I think reshape() of mprecip as a data frame will do it, or stack(), my closest attempts so far were: reshape(mprecip, direction="long", idvar="date", varying=list(names(mprecip)[2:7])) which gets "date" right, has the data in the right order, but has a "time" variable instead of the station names, or: stack(mprecip) which doesn't have the dates, but is otherwise in the expected order. The reshape() arguments are a black art ... Best wishes, Roger> > I have matrix (mprecip) with headers: > > > dim(mprecip) > [1] 6268 170 > > > mprecip > date GilzeRijen Eindhoven Volkel ZuidLimburg Arcen Ubachsberg > 1 01/01/1978 NA 0.0 NA 0.1 NA NA > 2 01/02/1978 NA 0.0 NA 0.0 NA NA > 3 01/03/1978 NA 1.9 NA 0.7 NA NA > 4 01/04/1978 NA 3.5 NA 6.9 NA 6.0 > 5 01/05/1978 NA 1.6 NA 1.8 NA 1.3 > 6 01/06/1978 NA 0.0 NA 0.0 NA NA > 7 01/07/1978 NA 0.0 NA 0.0 NA NA > 8 01/08/1978 NA 0.0 NA 0.0 NA NA > > Columns are: DATE and PRECIP values for each station listed in the header. > > I would like to transform the matrix into three columns (database like) > to be able to load the data in the database. Here is the output I would > like to get get it, number of rows = ncol x nrow. Output should look > like this: > > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > . > . > . > . > STATION_NAME2 DATE PRECIP > STATION_NAME2 DATE PRECIP > STATION_NAME2 DATE PRECIP > . > . > . > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > > > Thanks again for your help. > > > Rado > >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
On 5/18/06, Rado Bonk <rado.bonk at jrc.it> wrote:> Dear R-users > > Sorry for posting the previous message too soon before I have finished it. > > I have matrix (mprecip) with headers: > > > dim(mprecip) > [1] 6268 170 > > > mprecip > date GilzeRijen Eindhoven Volkel ZuidLimburg Arcen Ubachsberg > 1 01/01/1978 NA 0.0 NA 0.1 NA NA > 2 01/02/1978 NA 0.0 NA 0.0 NA NA > 3 01/03/1978 NA 1.9 NA 0.7 NA NA > 4 01/04/1978 NA 3.5 NA 6.9 NA 6.0 > 5 01/05/1978 NA 1.6 NA 1.8 NA 1.3 > 6 01/06/1978 NA 0.0 NA 0.0 NA NA > 7 01/07/1978 NA 0.0 NA 0.0 NA NA > 8 01/08/1978 NA 0.0 NA 0.0 NA NA > > Columns are: DATE and PRECIP values for each station listed in the header. > > I would like to transform the matrix into three columns (database like) > to be able to load the data in the database. Here is the output I would > like to get get it, number of rows = ncol x nrow. Output should look > like this: > > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > STATION_NAME1 DATE PRECIP > . > . > . > . > STATION_NAME2 DATE PRECIP > STATION_NAME2 DATE PRECIP > STATION_NAME2 DATE PRECIP > . > . > . > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > STATION_NAME170 DATE PRECIP > >Try this:> library(reshape) > melt(mprecip, 1, variable = "Station")date Station value 1 01/01/1978 GilzeRijen NA 2 01/02/1978 GilzeRijen NA 3 01/03/1978 GilzeRijen NA 4 01/04/1978 GilzeRijen NA 5 01/05/1978 GilzeRijen NA 6 01/06/1978 GilzeRijen NA 7 01/07/1978 GilzeRijen NA 8 01/08/1978 GilzeRijen NA 11 01/01/1978 Eindhoven 0.0 21 01/02/1978 Eindhoven 0.0 31 01/03/1978 Eindhoven 1.9 41 01/04/1978 Eindhoven 3.5 51 01/05/1978 Eindhoven 1.6 61 01/06/1978 Eindhoven 0.0 71 01/07/1978 Eindhoven 0.0 81 01/08/1978 Eindhoven 0.0 12 01/01/1978 Volkel NA 22 01/02/1978 Volkel NA 32 01/03/1978 Volkel NA 42 01/04/1978 Volkel NA 52 01/05/1978 Volkel NA 62 01/06/1978 Volkel NA 72 01/07/1978 Volkel NA 82 01/08/1978 Volkel NA 13 01/01/1978 ZuidLimburg 0.1 23 01/02/1978 ZuidLimburg 0.0 33 01/03/1978 ZuidLimburg 0.7 43 01/04/1978 ZuidLimburg 6.9 53 01/05/1978 ZuidLimburg 1.8 63 01/06/1978 ZuidLimburg 0.0 73 01/07/1978 ZuidLimburg 0.0 83 01/08/1978 ZuidLimburg 0.0 14 01/01/1978 Arcen NA 24 01/02/1978 Arcen NA 34 01/03/1978 Arcen NA 44 01/04/1978 Arcen NA 54 01/05/1978 Arcen NA 64 01/06/1978 Arcen NA 74 01/07/1978 Arcen NA 84 01/08/1978 Arcen NA 15 01/01/1978 Ubachsberg NA 25 01/02/1978 Ubachsberg NA 35 01/03/1978 Ubachsberg NA 45 01/04/1978 Ubachsberg 6.0 55 01/05/1978 Ubachsberg 1.3 65 01/06/1978 Ubachsberg NA 75 01/07/1978 Ubachsberg NA 85 01/08/1978 Ubachsberg NA