Hi, I apologize for the previous posting, where the message was not formatted properly. Here is a better version: I have written the following function to check whether a vector has elements satisfying monotonicity. is.monotone <- function(vec, increase=T){ ans <- TRUE vec.nomis <- vec[!is.na(vec)] if (increase & any(diff(vec.nomis,1) < 0, na.rm=T)) ans <- FALSE if (!increase & any(diff(vec.nomis,1) > 0, na.rm=T)) ans <- FALSE ans } This works correctly, but I get this error message as below.> x <- 2:10> is.monotone(x)[1] TRUE Warning messages: 1: the condition has length > 1 and only the first element will be used in: if (increase & any(diff(vec.nomis, 1) < 0, na.rm = T)) ans <- FALSE 2: the condition has length > 1 and only the first element will be used in: if (!increase & any(diff(vec.nomis, 1) > 0, na.rm = T)) ans <- FALSE>I am unable to see why the condition should have a length greater than 1, since "any" should give me a single logical value. Can any one tell me what is going on here? (I am using version 2.1.1 on Windows). Thanks very much, Ravi. -------------------------------------------------------------------------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: <mailto:rvaradhan@jhmi.edu> rvaradhan@jhmi.edu -------------------------------------------------------------------------- [[alternative HTML version deleted]]
Works fine for me with no warning messages. R for Windows 2.2.0. Try upgrading (but I don't see why you get the warning either). Try changing & to && though, as you don't need to vectorize the conjunction. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ravi Varadhan > Sent: Monday, November 21, 2005 3:17 PM > To: 'RHelp' > Subject: [R] Can't figure out warning message > > Hi, > > > > I apologize for the previous posting, where the message was > not formatted > properly. Here is a better version: > > > > I have written the following function to check whether a > vector has elements > satisfying monotonicity. > > > > is.monotone <- function(vec, increase=T){ > > ans <- TRUE > > vec.nomis <- vec[!is.na(vec)] > > if (increase & any(diff(vec.nomis,1) < 0, na.rm=T)) ans <- FALSE > > if (!increase & any(diff(vec.nomis,1) > 0, na.rm=T)) ans <- FALSE > > ans > > } > > > > This works correctly, but I get this error message as below. > > > > > x <- 2:10 > > > is.monotone(x) > > [1] TRUE > > Warning messages: > > 1: the condition has length > 1 and only the first element > will be used in: > if (increase & any(diff(vec.nomis, 1) < 0, na.rm = T)) ans <- FALSE > > 2: the condition has length > 1 and only the first element > will be used in: > if (!increase & any(diff(vec.nomis, 1) > 0, na.rm = T)) ans <- FALSE > > > > > > > I am unable to see why the condition should have a length > greater than 1, > since "any" should give me a single logical value. > > > > Can any one tell me what is going on here? (I am using > version 2.1.1 on > Windows). > > > > Thanks very much, > > Ravi. > > > > -------------------------------------------------------------- > ------------ > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > -------------------------------------------------------------- > ------------ > > > > > [[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 >
Ravi Varadhan wrote:> Hi, > > > > I apologize for the previous posting, where the message was not formatted > properly. Here is a better version: > > > > I have written the following function to check whether a vector has elements > satisfying monotonicity. > > > > is.monotone <- function(vec, increase=T){ > > ans <- TRUE > > vec.nomis <- vec[!is.na(vec)] > > if (increase & any(diff(vec.nomis,1) < 0, na.rm=T)) ans <- FALSE > > if (!increase & any(diff(vec.nomis,1) > 0, na.rm=T)) ans <- FALSE > > ans > > } > > > > This works correctly, but I get this error message as below. > > > >> x <- 2:10 > >> is.monotone(x) > > [1] TRUE > > Warning messages: > > 1: the condition has length > 1 and only the first element will be used in: > if (increase & any(diff(vec.nomis, 1) < 0, na.rm = T)) ans <- FALSE > > 2: the condition has length > 1 and only the first element will be used in: > if (!increase & any(diff(vec.nomis, 1) > 0, na.rm = T)) ans <- FALSE > >Try to double the &: && in place of & Kjetil> > > I am unable to see why the condition should have a length greater than 1, > since "any" should give me a single logical value. > > > > Can any one tell me what is going on here? (I am using version 2.1.1 on > Windows). > > > > Thanks very much, > > Ravi. > > > > -------------------------------------------------------------------------- > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > -------------------------------------------------------------------------- > > > > > [[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 >
Hi, I apologize for my hasty posting. The function works perfectly, after I realized that I had a matrix named "T" sitting in my workspace, so when I set my default option for "increase" to T, it created a problem. Changing my default to "TRUE" solved my problem. Thanks, Ravi.> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Ravi Varadhan > Sent: Monday, November 21, 2005 6:17 PM > To: 'RHelp' > Subject: [R] Can't figure out warning message > > Hi, > > > > I apologize for the previous posting, where the message was not formatted > properly. Here is a better version: > > > > I have written the following function to check whether a vector has > elements > satisfying monotonicity. > > > > is.monotone <- function(vec, increase=T){ > > ans <- TRUE > > vec.nomis <- vec[!is.na(vec)] > > if (increase & any(diff(vec.nomis,1) < 0, na.rm=T)) ans <- FALSE > > if (!increase & any(diff(vec.nomis,1) > 0, na.rm=T)) ans <- FALSE > > ans > > } > > > > This works correctly, but I get this error message as below. > > > > > x <- 2:10 > > > is.monotone(x) > > [1] TRUE > > Warning messages: > > 1: the condition has length > 1 and only the first element will be used > in: > if (increase & any(diff(vec.nomis, 1) < 0, na.rm = T)) ans <- FALSE > > 2: the condition has length > 1 and only the first element will be used > in: > if (!increase & any(diff(vec.nomis, 1) > 0, na.rm = T)) ans <- FALSE > > > > > > > I am unable to see why the condition should have a length greater than 1, > since "any" should give me a single logical value. > > > > Can any one tell me what is going on here? (I am using version 2.1.1 on > Windows). > > > > Thanks very much, > > Ravi. > > > > -------------------------------------------------------------------------- > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > -------------------------------------------------------------------------- > > > > > [[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
My guess is that you have an object 'T' hanging around. Like Berton, I get no error until I define: T <- 0:1 and then run your test case, resulting in the warnings. Peter Ravi Varadhan wrote:> Hi, > > > > I apologize for the previous posting, where the message was not formatted > properly. Here is a better version: > > > > I have written the following function to check whether a vector has elements > satisfying monotonicity. > > > > is.monotone <- function(vec, increase=T){ > > ans <- TRUE > > vec.nomis <- vec[!is.na(vec)] > > if (increase & any(diff(vec.nomis,1) < 0, na.rm=T)) ans <- FALSE > > if (!increase & any(diff(vec.nomis,1) > 0, na.rm=T)) ans <- FALSE > > ans > > } > > > > This works correctly, but I get this error message as below. > > > > >>x <- 2:10 > > >>is.monotone(x) > > > [1] TRUE > > Warning messages: > > 1: the condition has length > 1 and only the first element will be used in: > if (increase & any(diff(vec.nomis, 1) < 0, na.rm = T)) ans <- FALSE > > 2: the condition has length > 1 and only the first element will be used in: > if (!increase & any(diff(vec.nomis, 1) > 0, na.rm = T)) ans <- FALSE > > > > > > I am unable to see why the condition should have a length greater than 1, > since "any" should give me a single logical value. > > > > Can any one tell me what is going on here? (I am using version 2.1.1 on > Windows). > > > > Thanks very much, > > Ravi. > > > > -------------------------------------------------------------------------- > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > -------------------------------------------------------------------------- > > > > > [[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-- Peter Ehlers Department of Mathematics and Statistics University of Calgary, 2500 University Dr. NW ph: 403-220-3936