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 n
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 warnin
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 som
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
___
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 D
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:
$ d
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
> as.matrix.da
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 c
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) a
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
>
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 onl
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 t
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)
-
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 mak
14 matches
Mail list logo