[R] There is a relationship of the Modify-in-place optimisation of a object and the local variable `*tmp*`?

2021-10-08 Thread Ben Deivide de Oliveira Batista
Dear R users, When modify-in-place of objects occurs, is there a local variable called `*tmp*`, behind the scenes R? Let's look at two examples to understand the question. Example 1 (R Language Definition) -- > x <- 1:10 > tracemem(x) [1]

Re: [R] adding results to plot

2021-10-08 Thread Rui Barradas
Hello, Thanks for the compliment. The R Core team, to which we must be very grateful for their great work along so many years, is, for good and obvious reasons, known to be change resistant and a new method would overload them even more with maintenance worries so I guess text.htest won't

Re: [R] There is a relationship of the Modify-in-place optimisation of a object and the local variable `*tmp*`?

2021-10-08 Thread Duncan Murdoch
On 07/10/2021 6:46 p.m., Ben Deivide de Oliveira Batista wrote: Dear R users, When modify-in-place of objects occurs, is there a local variable called `*tmp*`, behind the scenes R? Let's look at two examples to understand the question. Example 1 (R Language Definition)

Re: [R] [EXTERNAL] Re: unexpected behavior in apply

2021-10-08 Thread Derickson, Ryan, VHA NCOD via R-help
This is interesting and does seem suboptimal. Especially because if I start with a matrix from the beginning, it behaves as expected. > d<-data.frame(d1 = letters[1:3], + d2 = c("1","2","3"), + d3 = c(NA,NA,"6")) > > str(d) 'data.frame': 3 obs. of 3 variables: $

[R] ODP: unexpected behavior in apply

2021-10-08 Thread Grzegorz Smoliński
Hi, but why is there a space before 6? Isn't it the source of the problem? Best regards, Grzegorz pt., 8 paź 2021 o 20:14 Andrew Simmons napisał(a): > > Hello, > > > The issue comes that 'apply' tries to coerce its argument to a matrix. This > means that all your columns will become character

[R] unexpected behavior in apply

2021-10-08 Thread Derickson, Ryan, VHA NCOD via R-help
Hello, I'm seeing unexpected behavior when using apply() compared to a for loop when a character vector is part of the data subjected to the apply statement. Below, I check whether all non-missing values are <= 3. If I include a character column, apply incorrectly returns TRUE for d3. If I

Re: [R] unexpected behavior in apply

2021-10-08 Thread Jiefei Wang
Hi, I guess this can tell you what happens behind the scene > d<-data.frame(d1 = letters[1:3], + d2 = c(1,2,3), + d3 = c(NA,NA,6)) > apply(d, 2, FUN=function(x)x) d1 d2 d3 [1,] "a" "1" NA [2,] "b" "2" NA [3,] "c" "3" " 6" > "a"<=3 [1] FALSE > "2"<=3 [1] TRUE >

Re: [R] unexpected behavior in apply

2021-10-08 Thread Andrew Simmons
Hello, The issue comes that 'apply' tries to coerce its argument to a matrix. This means that all your columns will become character class, and the result will not be what you wanted. I would suggest something more like: sapply(d, function(x) all(x[!is.na(x)] <= 3)) or vapply(d, function(x)

Re: [R] unexpected behavior in apply

2021-10-08 Thread Jiefei Wang
Ok, it turns out that this is documented, even though it looks surprising. First of all, the apply function will try to convert any object with the dim attribute to a matrix(my intuition agrees with you that there should be no conversion), so the first step of the apply function is >

Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread John Fox
Dear Ravi, On 2021-10-08 8:21 a.m., Ravi Varadhan wrote: Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but I felt that there must be a more principled approach.  While John's solution is what I would prefer, I cannot help but wonder why `ifelse' was not constructed

Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread Avi Gross via R-help
Ravi, I have no idea what motivated the people who made ifelse() but there is no reason they felt the need to program it to meet your precise need. As others have noted, it probably was built to handle simple cases well and it expects to return a result that is the same length as the input. If

Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread Jeff Newmiller
I don't think it is avoidable... ifelse cannot figure out which elements will exist in the second and third arguments until they are evaluated, so it cannot make use of the TRUE/FALSE information until both possible output vectors have been evaluated, which is a performance hit and yields

Re: [R] [EXTERNAL] Re: unexpected behavior in apply

2021-10-08 Thread Grzegorz Smoliński
This will work as well: d<-data.frame(d1 = letters[1:3], d2 = c(1,2,3), d3 = c(NA_character_,NA_character_,6)) apply(d, 2, FUN=function(x)all(x[!is.na(x)] <= 3)) d1 d2 d3 FALSE TRUE FALSE i.e. when NA changed do NA_character_ pt., 8 paź 2021 o 20:44

Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread Ravi Varadhan via R-help
Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but I felt that there must be a more principled approach. While John's solution is what I would prefer, I cannot help but wonder why `ifelse' was not constructed to avoid this behavior. Thanks & Best regards, Ravi

Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread Deepayan Sarkar
On Sat, Oct 9, 2021 at 3:00 AM Ravi Varadhan via R-help wrote: > > Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but > I felt that > there must be a more principled approach. While John's solution is what I > would prefer, > I cannot help but wonder why `ifelse' was