bickis m@iii@g oii m@th@us@sk@c@
2019-Sep-11 20:51 UTC
[R] Strange behaviour of sapply function.
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
Can you create a reproducible example? You don't show: income, atv, sktaxb, sktax On Thu, Sep 12, 2019 at 12:04 PM <bickis at math.usask.ca> 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 > > ______________________________________________ > R-help at r-project.org 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]]
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 <bickis at math.usask.ca> 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 > > ______________________________________________ > R-help at r-project.org 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.
Quoting bickis at math.usask.ca:> 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
bickis m@iii@g oii m@th@us@sk@c@
2019-Sep-12 15:12 UTC
[R] Strange behaviour of sapply function.
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 bickis at math.usask.ca: > >> 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 > >