Strange behaviour of sapply function.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange behaviour of sapply function.

bickis
Here is are a few lines of my R session:

> class(income)
[1] "integer"
> class(sapply(1000*income-999,atv,sktaxb,sktax))
[1] "numeric"
> class(sapply(1000*income-1001,atv,sktaxb,sktax))
[1] "list"

Although "income" is a numeric array, and sapply works as expected
returning an array (the function "atv" returns a single numeric argument),
if subtract a large enough number from the first argument, the sapply
function now wants to return a list?   Am I missing something?

I am running version 3.3.2 on Mac OS 10.9.9

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of sapply function.

Eric Berger
Can you create a reproducible example?
You don't show: income, atv, sktaxb, sktax



On Thu, Sep 12, 2019 at 12:04 PM <[hidden email]> wrote:

> Here is are a few lines of my R session:
>
> > class(income)
> [1] "integer"
> > class(sapply(1000*income-999,atv,sktaxb,sktax))
> [1] "numeric"
> > class(sapply(1000*income-1001,atv,sktaxb,sktax))
> [1] "list"
>
> Although "income" is a numeric array, and sapply works as expected
> returning an array (the function "atv" returns a single numeric argument),
> if subtract a large enough number from the first argument, the sapply
> function now wants to return a list?   Am I missing something?
>
> I am running version 3.3.2 on Mac OS 10.9.9
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of sapply function.

Jim Lemon-4
In reply to this post by bickis
Hi bickis,
Putting on my dark glasses and flailing about with a big white stick*,
I would suggest that you look at what "atv" actually produces from
those three objects. I wouldn't be surprised to find quite different
things.

Jim
* blind guess

On Thu, Sep 12, 2019 at 7:04 PM <[hidden email]> wrote:

>
> Here is are a few lines of my R session:
>
> > class(income)
> [1] "integer"
> > class(sapply(1000*income-999,atv,sktaxb,sktax))
> [1] "numeric"
> > class(sapply(1000*income-1001,atv,sktaxb,sktax))
> [1] "list"
>
> Although "income" is a numeric array, and sapply works as expected
> returning an array (the function "atv" returns a single numeric argument),
> if subtract a large enough number from the first argument, the sapply
> function now wants to return a list?   Am I missing something?
>
> I am running version 3.3.2 on Mac OS 10.9.9
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of sapply function.

Enrico Schumann-2
In reply to this post by bickis

Quoting [hidden email]:

> Here is are a few lines of my R session:
>
>> class(income)
> [1] "integer"
>> class(sapply(1000*income-999,atv,sktaxb,sktax))
> [1] "numeric"
>> class(sapply(1000*income-1001,atv,sktaxb,sktax))
> [1] "list"
>
> Although "income" is a numeric array, and sapply works as expected
> returning an array (the function "atv" returns a single numeric argument),
> if subtract a large enough number from the first argument, the sapply
> function now wants to return a list?   Am I missing something?
>
> I am running version 3.3.2 on Mac OS 10.9.9
>

You have not shown what 'income', 'atv', and so on are; so there is an  
infinity
of possible reasons why you get a list instead of a numeric vector.

One possible reason: what if 'atv' sometimes returns no value at all?

f <- function(x) x[x>0]
str(sapply(1:10, f))
## int [1:10] 1 2 3 4 5 6 7 8 9 10

str(sapply(-5:5, f))
## List of 11
##  $ : int(0)
##  $ : int(0)
##  $ : int(0)
##  $ : int(0)
##  $ : int(0)
##  $ : int(0)
##  $ : int 1
##  $ : int 2
##  $ : int 3
##  $ : int 4
##  $ : int 5

--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of sapply function.

bickis
Thanks.    You are right.  I have realized that the atv function returns
empty for negative arguments.  I was not aware that this would affect how
sapply processes its result.

>
> Quoting [hidden email]:
>
>> Here is are a few lines of my R session:
>>
>>> class(income)
>> [1] "integer"
>>> class(sapply(1000*income-999,atv,sktaxb,sktax))
>> [1] "numeric"
>>> class(sapply(1000*income-1001,atv,sktaxb,sktax))
>> [1] "list"
>>
>> Although "income" is a numeric array, and sapply works as expected
>> returning an array (the function "atv" returns a single numeric
>> argument),
>> if subtract a large enough number from the first argument, the sapply
>> function now wants to return a list?   Am I missing something?
>>
>> I am running version 3.3.2 on Mac OS 10.9.9
>>
>
> You have not shown what 'income', 'atv', and so on are; so there is an
> infinity
> of possible reasons why you get a list instead of a numeric vector.
>
> One possible reason: what if 'atv' sometimes returns no value at all?
>
> f <- function(x) x[x>0]
> str(sapply(1:10, f))
> ## int [1:10] 1 2 3 4 5 6 7 8 9 10
>
> str(sapply(-5:5, f))
> ## List of 11
> ##  $ : int(0)
> ##  $ : int(0)
> ##  $ : int(0)
> ##  $ : int(0)
> ##  $ : int(0)
> ##  $ : int(0)
> ##  $ : int 1
> ##  $ : int 2
> ##  $ : int 3
> ##  $ : int 4
> ##  $ : int 5
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.