Re: [R] matrix transformation into 3 columns II.

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Fri 19 May 2006 - 03:20:28 EST


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@nhh.no

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri May 19 03:23:47 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 19 May 2006 - 04:10:14 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.