[R] Confused by code?

Peter Alspach Peter.Alspach at plantandfood.co.nz
Mon Sep 24 02:02:33 CEST 2012


Tena koe

I think you probably meant:
x[as.logical(z)] <- y[as.logical(z)]

i.e., choosing those elements of × and y where z is 1 (TRUE as logical).  Whereas what you have written:

×[z] <- y[z]

references the 0th (by default indexing starts at 1 so this is empty (see ×[0]) and the first element of × and y (repeatedly).

Hope this helps ....

Peter Alspach

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bazman76
Sent: Monday, 24 September 2012 8:53 a.m.
To: r-help at r-project.org
Subject: [R] Confused by code?

x<-matrix(c(1,0,0,0,1,0,0,0,1),nrow=3)
> y<-matrix(c(0,0,0,1,0,0,1,1,0),nrow=3)
> z<-matrix(c(0,1,0,0,1,0,1,0,0),nrow=3)
> x[z]<-y[z]

The resultant matrix x is all zeros except for the last two diagonal cells which are 1's.
While y is lower triangualr 0's with the remaining cells all ones.

I really don't understand how this deceptively simple looking piece of code is giving that result can someone explain please.
I'm obviously missing something pretty basic so please keep your answer suitably basic.



--
View this message in context: http://r.789695.n4.nabble.com/Confused-by-code-tp4643946.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.

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.




More information about the R-help mailing list