[R] Rearranging a data frame for multiple observations

David Winsemius dwinsemius at comcast.net
Sat Feb 20 17:31:45 CET 2010


Henrique;

I wonder if you can comment on why that works as it does. I have tried  
to pull it apart and reassemble it piece by piece, but always end up  
with argument recycling.

 > do.call(rbind, c(unstack(x, V2 ~ V1)[1], unstack(x, V2 ~ V1)[3]))
            [,1]       [,2]       [,3]       [,4]       [,5]
CAO0337134 "05/09/95" "27/09/05" "05/09/95" "27/09/05" "05/09/95"
CAO0367128 "11/07/05" "12/07/05" "14/07/05" "19/09/97" "20/09/97"

(Further efforts with list() instead of c() were unrewarding in  
various ways.)

 > do.call(rbind, list(unstack(x, V2 ~ V1)[1], unstack(x, V2 ~ V1)[3]))
      CAO0337134
[1,] Character,2
[2,] Character,5


What is it about this version that preserves the missingness when  
using "["?

 > do.call(rbind, lapply(unstack(x, V2 ~ V1), '[', 1:5))
            [,1]       [,2]       [,3]       [,4]       [,5]
CAO0337134 "05/09/95" "27/09/05" NA         NA         NA
CAO0347741 "10/10/04" "12/10/04" NA         NA         NA
CAO0367128 "11/07/05" "12/07/05" "14/07/05" "19/09/97" "20/09/97"
CAO0374110 "14/11/89" "17/11/89" NA         NA         NA
CAO0374779 "01/10/99" "28/09/99" "29/09/99" NA         NA

-- 
David.

On Feb 20, 2010, at 10:37 AM, Henrique Dallazuanna wrote:

> Try this:
>
> do.call(rbind, lapply(unstack(x, V2 ~ V1), '[', 1:max(with(x,
> tapply(V2, V1, length)))))
>
> On Sat, Feb 20, 2010 at 9:26 AM, Newbie19_02 <nvanzuydam at gmail.com>  
> wrote:
>>
>> Hi there,
>>
>> I think I'm struggling with a fairly simple problem but can't seem  
>> to solve
>> it.  I have multiple observations for one unique identifier.   
>> Ultimately I
>> want to end up with one line per identifier with multiple  
>> observations in
>> rows. I'm really stuck any help would be really appreciated.
>>
>>
>> CAO0337134      05/09/95
>> CAO0337134      27/09/05
>> CAO0347741      10/10/04
>> CAO0347741      12/10/04
>> CAO0367128      11/07/05
>> CAO0367128      12/07/05
>> CAO0367128      14/07/05
>> CAO0367128      19/09/97
>> CAO0367128      20/09/97
>> CAO0374110      14/11/89
>> CAO0374110      17/11/89
>> CAO0374779      01/10/99
>> CAO0374779      28/09/99
>> CAO0374779      29/09/99
>>
>> Thanks
>> Natalie
>> --
>> View this message in context: http://n4.nabble.com/Rearranging-a-data-frame-for-multiple-observations-tp1562780p1562780.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> 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