Re: [R] Assignment from list

2007-04-12 Thread Dimitris Rizopoulos
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" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
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
> day18
> svn rev40228
> 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: [EMAIL PROTECTED]
> Tel: +44 (0)20 7848 6532
> Fax: +44 (0)20 7848 6550
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> [EMAIL PROTECTED] 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

__
[EMAIL PROTECTED] 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.


[R] Assignment from list

2007-04-12 Thread Eric Blanc
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
day18
svn rev40228
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: [EMAIL PROTECTED]
Tel: +44 (0)20 7848 6532
Fax: +44 (0)20 7848 6550




[[alternative HTML version deleted]]

__
[EMAIL PROTECTED] 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.