[R] strange dlply behavior

hadley wickham h.wickham at gmail.com
Wed Jul 22 04:09:31 CEST 2009


Hi Damien,

This is because of a small bug.  You can work around it by explicitly
using the force function - dlply(d, "V1", force).  The default will be
fixed in the next version.

Regards,

Hadley

On Tue, Jul 21, 2009 at 11:18 AM, Damien Moore<damienlmoore at gmail.com> wrote:
> I'm running R 2.9.1 on winXP, using the library plyr.
>
> Can anyone explain to me what is going wrong in this code? (in particular
> see lines marked with ******************) Trying to modify objects in a list
> created using dlply seems to corrupt the objects in the list.
>
>> library(plyr)
>> d=as.data.frame(cbind(c(1,1,1,2,2,2),c(1,2,3,4,5,6)))
>> d
>  V1 V2
> 1  1  1
> 2  1  2
> 3  1  3
> 4  2  4
> 5  2  5
> 6  2  6
>> c=dlply(d,.(V1))
>> c
> [[1]]
>  V1 V2
> 1  1  1
> 2  1  2
> 3  1  3
>
> [[2]]
>  V1 V2
> 4  2  4
> 5  2  5
> 6  2  6
>
> ## display an element from the second data frame
>> c[[2]][2,2]
> [1] 5
>
> ## change element in the second data from
>> c[[2]][2,2]=10
>> c
> [[1]]
>    V1 V2
> 2    1  2    **************
> 2.1  1  2   **************  What happened to V2?
> 2.2  1  2   **************
>
> [[2]]
>   V1 V2
> 4   2  4
> NA NA NA **********************************
> 6   2  6
>
> ##Try again with first data frame
>> c=dlply(d,.(V1))
>> c[[1]][2,2]=10 **********************************
>> c
> [[1]]
> NULL ********************************* YIKES!
>
>
> ##Try again but copy c into a new list k
>> c=dlply(d,.(V1))
>> k=list(c[[1]],c[[2]])
>> k[[1]]
>  V1 V2
> 1  1  1
> 2  1  2
> 3  1  3
>> k[[2]][2,2]=10
>> k
> [[1]]
>  V1 V2
> 1  1  1
> 2  1  2
> 3  1  3
>
> [[2]]
>  V1 V2
> 4  2  4
> 5  2 10 ***************************
> 6  2  6
>> k[[1]][2,2]=10
>> k
> [[1]]
>  V1 V2
> 1  1  1
> 2  1 10 *******************************
> 3  1  3
>
> [[2]]
>  V1 V2
> 4  2  4
> 5  2 10
> 6  2  6
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
http://had.co.nz/




More information about the R-help mailing list