[R] list to balanced array

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Aug 16 17:34:39 CEST 2006


try the following:

arrivals <- matrix(sample(1:24, 100, TRUE), 10, 10)
apply(arrivals, 2, function(x) table(factor(x, levels = 1:24)))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Spencer Jones" <ssj1364 at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 16, 2006 5:22 PM
Subject: [R] list to balanced array


>I am working with a large data set of arrivals, for each day I have
> aggregated the arrivals into hrs (1-24) via: apply(x,2,table). On 
> some days
> there are zero arrivals during some hours of the day, this leaves me 
> with
> (I believe) a list of vectors of differnt lengths (see below).
>
>
> [[4]]
>
> 1  2  3  5  6  8  9 10 11 13 14 15 16 17 18 19 20 21 22 23 24
> 1  3  2  3  1  1  2  3   4   4   4   3   2   6  2   5   1  2   2   2 
> 1
>
> [[5]]
>
> 2  5  6  8  9 10 11 12 13 14 15 16 17 18 19 20 22 23 24
> 2  1  1  2  1   5   3   6   6  3   2   2  1   4   3   3  4   2   1
>
> I would like to be able to create an array with equal numbers of 
> rows (24)
> for each column, i.e., fill in the gaps with Zeros.
>
>
> [[5]]
>
> 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20  21  22 
> 23 24
> 0  2  0  0  1  1  0  2  1   5   3   6   6  3   2   2  1   4   3   0 
> 3
> 4   2   1
>
>
> Any suggestions?
>
>
> thanks,
>
> Spencer
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list