[R] Assignment from list

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Apr 12 10:55:20 CEST 2007


try the following:

z <- matrix(1, ncol = 249, nrow = 240)
zz <- matrix(1, ncol = 249, nrow = 240)
for (k in seq_along(x)) {
    z[rbind(x[[k]])] <- 0
}
zz[xx] <- 0

all.equal(z, zz)


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: "Eric Blanc" <eric.blanc at kcl.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, April 12, 2007 10:36 AM
Subject: [R] Assignment from list


>I have a list of groups of xy positions I want to set to 0 in an
> array full of 1s. When the assignments are done directly from the
> list, they are incorrect, while if I use a temporary array derived
> from the list the assignments are correct. The following example 
> will
> hopefully make my problem clearer.
>
> The matrices z and zz are initialised with 1. The z and zz values at
> coordinates in list x are then set to 0 using two different methods.
> Although either method should lead to the same results, z and zz are
> different. z is incorrect, because some values in its first row have
> been incorrectly set to 0.
>
> x  <- list( matrix( c( 9, 9, 74, 75 ), nrow=2 ),
>             matrix( c( 11, 11, 34, 35 ), nrow=2 ),
>             matrix( c( 14, 15, 58, 58 ), nrow=2 ),
>             c( 16, 142 ),
>             matrix( c( 19, 19, 94, 95 ), nrow=2 ),
>             matrix( c( 19, 20, 127, 127 ), nrow=2 ),
>             matrix( c( 22, 22, 112, 113 ), nrow=2 ),
>             c( 23, 13 ),
>             matrix( c( 26, 27, 81, 81 ), nrow=2 ),
>             matrix( c( 31, 32, 153, 153 ), nrow=2 ) )
> xx <- do.call( "rbind", x )
>
> z <- matrix( 1, ncol=249, nrow=240 )
> zz <- matrix( 1, ncol=249, nrow=240 )
>
> for( k in x ) z[k] <- 0
> zz[xx] <- 0
>
> sum( z != zz )
> c( sum( z[,1] != 1 ), sum( zz[,1] != 1 ) )
>
> Somewhere, I must be doing something wrong, or assuming something
> incorrect. I would be very grateful if anybody could point me in the
> right direction.
>
> platform       i386-apple-darwin8.8.1
> arch           i386
> os             darwin8.8.1
> system         i386, darwin8.8.1
> status
> major          2
> minor          4.1
> year           2006
> month          12
> day            18
> svn rev        40228
> language       R
> version.string R version 2.4.1 (2006-12-18)
>
> (same results with platform i386-pc-mingw32, R version 2.4.1 and
> platform x86_64-unknown-linux-gnu, R version 2.3.1)
>
> --
> Dr Eric Blanc
> Lecturer in Bioinformatics
> MRC Centre for Developmental Neurobiology
> King's College London
> New Hunt's House Room 4.10B
> Guy's Hospital Campus
> London SE1 1UL
>
> E-mail: eric.blanc at kcl.ac.uk
> Tel: +44 (0)20 7848 6532
> Fax: +44 (0)20 7848 6550
>
>
>
>
> [[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