vincent.stoliaroff@socgen.com
2003-Apr-25 15:15 UTC
[R] Code bug unresolved involving if condition
Hi R lovers! I am a beginner in coding with R so my question may be very easily solved but I don't know how. I have written the following function in a .txt file ClearDelta <- function(Matrix) { ncol<-ncol(Matrix);nrow<-nrow(Matrix); for (i in 1:nrow) { for (j in 1:(ncol-1)) {if (Matrix[i,j]==NA) (NA->Matrix[i,j+1])} } } I can charge it with the source() command But I get the following message when applied to a matrix> ClearDelta(MatCor)Error in if (Matrix[i, j] == NA) (Matrix[i, j + 1] <- NA) : missing value where logical needed MatCor is the following Matrix> MatCor[,1] [,2] [,3] [1,] NA 0.9870676 0.04648933 [2,] 0.98706757 1.0000000 -0.17353590 [3,] 0.04648933 -0.1735359 1.00000000 Do you know why I get such an unpleasant message from so polite a software? Thanks to anybody who could help. ************************************************************************* Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. Toute utilisation ou diffusion non autorisee est interdite. Tout message electronique est susceptible d'alteration. La SOCIETE GENERALE et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. ******** This message and any attachments (the "message") are confidentia... {{dropped}}
The message is both pleasant and accurate! It indicates that you have made an error, and tells you what the error is. foo == NA is always missing, since NA denotes a missing value. Use is.na(foo): see ?is.na. On Fri, 25 Apr 2003 vincent.stoliaroff at socgen.com wrote:> Hi R lovers! > > I am a beginner in coding with R so my question may be very easily solved > but I don't know how. > > I have written the following function in a .txt file > > > ClearDelta <- function(Matrix) > { > ncol<-ncol(Matrix);nrow<-nrow(Matrix); > for (i in 1:nrow) { > for (j in 1:(ncol-1)) > {if (Matrix[i,j]==NA) (NA->Matrix[i,j+1])} > } > } > > I can charge it with the source() command > But I get the following message when applied to a matrix > > > ClearDelta(MatCor) > Error in if (Matrix[i, j] == NA) (Matrix[i, j + 1] <- NA) : > missing value where logical needed > > MatCor is the following Matrix > > > MatCor > [,1] [,2] [,3] > [1,] NA 0.9870676 0.04648933 > [2,] 0.98706757 1.0000000 -0.17353590 > [3,] 0.04648933 -0.1735359 1.00000000 > > Do you know why I get such an unpleasant message from so polite a software? > > Thanks to anybody who could help.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
vincent.stoliaroff@socgen.com
2003-Apr-25 15:45 UTC
[R] Code bug unresolved involving if condition
Thanks a lot I had corrected that already after thinking for a while about the cause of the bug However I still have some trouble in trying to set the Matrix[i,j+1] term to Missingness. I am not sure how to use the generic function `is.na<-' which sets elements to `NA'. {if (is.na(Matrix[i,j])) (is.na(Matrix[i,j+1])<-1)} does not work to set my Matrix[i,j+1] term to NA Thanks in advance for any help |---------+----------------------------> | | ripley at stats.ox.a| | | c.uk | | | | | | 04/25/03 05:34 PM| | | | |---------+----------------------------> >------------------------------------------------------------------------------------------------------------------------------| | | | To: Vincent STOLIAROFF/fr/socgen at socgen | | cc: r-help at stat.math.ethz.ch | | Subject: Re: [R] Code bug unresolved involving if condition | >------------------------------------------------------------------------------------------------------------------------------| The message is both pleasant and accurate! It indicates that you have made an error, and tells you what the error is. foo == NA is always missing, since NA denotes a missing value. Use is.na(foo): see ?is.na. On Fri, 25 Apr 2003 vincent.stoliaroff at socgen.com wrote:> Hi R lovers! > > I am a beginner in coding with R so my question may be very easily solved > but I don't know how. > > I have written the following function in a .txt file > > > ClearDelta <- function(Matrix) > { > ncol<-ncol(Matrix);nrow<-nrow(Matrix); > for (i in 1:nrow) { > for (j in 1:(ncol-1)) > {if (Matrix[i,j]==NA) (NA->Matrix[i,j+1])} > } > } > > I can charge it with the source() command > But I get the following message when applied to a matrix > > > ClearDelta(MatCor) > Error in if (Matrix[i, j] == NA) (Matrix[i, j + 1] <- NA) : > missing value where logical needed > > MatCor is the following Matrix > > > MatCor > [,1] [,2] [,3] > [1,] NA 0.9870676 0.04648933 > [2,] 0.98706757 1.0000000 -0.17353590 > [3,] 0.04648933 -0.1735359 1.00000000 > > Do you know why I get such an unpleasant message from so polite a software? > > Thanks to anybody who could help.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 ************************************************************************* Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. Toute utilisation ou diffusion non autorisee est interdite. Tout message electronique est susceptible d'alteration. La SOCIETE GENERALE et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. ******** This message and any attachments (the "message") are confidentia... {{dropped}}
On Fri, 25 Apr 2003 vincent.stoliaroff at socgen.com wrote:> Hi R lovers! > > I am a beginner in coding with R so my question may be very easily solved > but I don't know how. > > I have written the following function in a .txt file > > > ClearDelta <- function(Matrix) > { > ncol<-ncol(Matrix);nrow<-nrow(Matrix); > for (i in 1:nrow) { > for (j in 1:(ncol-1)) > {if (Matrix[i,j]==NA) (NA->Matrix[i,j+1])} > } > } > > I can charge it with the source() command > But I get the following message when applied to a matrix > > > ClearDelta(MatCor) > Error in if (Matrix[i, j] == NA) (Matrix[i, j + 1] <- NA) : > missing value where logical needed >> > Do you know why I get such an unpleasant message from so polite a software? >The test (Matrix[i,j] ==NA) returns NA, not TRUE or FALSE as you expected. Think of NA as meaning "I don't know what this value is", so you are asking if Matrix[i,j] is equal to a number that you don't know. The answer is that R doesn't know whether this is TRUE or FALSE, The value NA in a logical variable means just that "This is TRUE or FALSE but I don't know which". You can use is.na() test for NA, ie if(is.na(Matrix[i, j])) I can't resist also pointing out that for large matrices there is a more efficient answer for(i in 1:nrow(Matrix)) Matrix[i,]<- diff(c(0,cumsum(Matrix[i,]))) -thomas