Re: [R] unexpected behavior in apply

2021-10-13 Thread Derickson, Ryan, VHA NCOD via R-help
er. But I appreciate the explanation and the solutions. -Original Message- From: PIKAL Petr Sent: Monday, October 11, 2021 5:15 AM To: Jiefei Wang ; Derickson, Ryan, VHA NCOD Cc: r-help@r-project.org Subject: [EXTERNAL] RE: [R] unexpected behavior in apply Hi it is not surprising

Re: [R] unexpected behavior in apply

2021-10-11 Thread Rolf Turner
On Mon, 11 Oct 2021 09:15:27 + PIKAL Petr wrote: > > data.frame is not matrix or array (even if it rather resembles one) > > So if you put a cake into oven you cannot expect getting fried > potatoes from it. Another fortune nomination! cheers, Rolf -- Honorary Research Fellow

Re: [R] unexpected behavior in apply

2021-10-11 Thread PIKAL Petr
SE Cheers Petr > -Original Message- > From: R-help On Behalf Of Jiefei Wang > Sent: Friday, October 8, 2021 8:22 PM > To: Derickson, Ryan, VHA NCOD > Cc: r-help@r-project.org > Subject: Re: [R] unexpected behavior in apply > > Ok, it turns out that this is doc

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] 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
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 >

[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

[R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Luca Nanetti
Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's say test, dim=c(7,5). test - array(1:35, dim=c(7, 5)) test [,1] [,2] [,3] [,4] [,5]

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Duncan Murdoch
On 13-05-14 4:52 AM, Luca Nanetti wrote: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's say test, dim=c(7,5). test - array(1:35,

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Enrico Schumann
On Tue, 14 May 2013, Luca Nanetti luca.nane...@gmail.com writes: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's say test, dim=c(7,5).

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Rui Barradas
Hello, The problem is that apply returns the results vector by vector and in R vectors are column vectors. This is not exclusive of apply with sample as the function to be called, but of apply in general. Try, for instance apply(test, 1, identity) # transposes the array The rows are

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Ted Harding
On 14-May-2013 09:46:32 Duncan Murdoch wrote: On 13-05-14 4:52 AM, Luca Nanetti wrote: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Gabor Grothendieck
On Tue, May 14, 2013 at 4:52 AM, Luca Nanetti luca.nane...@gmail.com wrote: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I thought it could be interesting. I have an array, let's say test,

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Tsjerk Wassenaar
t(apply(test,1,sample)) will also do. As the OP noted, the results are simply transposed. So if an operation is to be applied to rows, yielding modified rows, simply transpose the results. Cheers, Tsjerk On Tue, May 14, 2013 at 12:07 PM, Ted Harding ted.hard...@wlandres.netwrote: On

Re: [R] Unexpected behavior of apply when FUN=sample

2013-05-14 Thread Patrick Burns
This is Circle 8.1.47 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 14/05/2013 09:52, Luca Nanetti wrote: Dear experts, I wanted to signal a peculiar, unexpected behaviour of 'apply'. It is not a bug, it is per spec, but it is so counterintuitive that I