Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example is not equivalent #15

Open
nacnudus opened this issue Jul 15, 2019 · 4 comments
Open

Example is not equivalent #15

nacnudus opened this issue Jul 15, 2019 · 4 comments

Comments

@nacnudus
Copy link

Yet the above lengthy R code can be done much more compactly. E.g

with(mtcars,tapply(mpg,list(cyl,gear),function(x) c(mean(x),sd(x)))

with output

     3      4    5
4 21.50 26.925 28.2
6 19.75 19.750 19.7
8 15.05     NA 15.4

Not only is the code highly compact, but also the output is nicer, in
table form.

This is not equivalent in an important way: the table form is difficult to use in subsequent commands. For example, try filtering the table for the combination of cyl and mpg that has the highest mean mpg.

@nacnudus
Copy link
Author

Also it doesn't render the quoted output.

with(mtcars,tapply(mpg,list(cyl,gear),function(x) c(mean(x),sd(x))))
#>   3         4         5
#> 4 Numeric,2 Numeric,2 Numeric,2
#> 6 Numeric,2 Numeric,2 Numeric,2
#> 8 Numeric,2 NULL      Numeric,2

Your output seems to the be result of applying only mean() and not sd().

with(mtcars,tapply(mpg,list(cyl,gear),function(x) c(mean(x))))
#>       3      4    5
#> 4 21.50 26.925 28.2
#> 6 19.75 19.750 19.7
#> 8 15.05     NA 15.4

@matloff
Copy link
Owner

matloff commented Jul 15, 2019

If one assigns the result to z, then which.max(z) does work. However, it gives 7, saying the 7th element is the largest, which may not be so convenient. I assume there is a table helper function to deal with this, but I'm not deep enough into tables to say.

As to displaying only the means, that's of course why I wrote "E.g." One could do 3 with() calls, or use tapply() with a custom function.

@nacnudus
Copy link
Author

As to displaying only the means, my point is that

with(mtcars,tapply(mpg,list(cyl,gear),function(x) c(mean(x),sd(x)))

does not output

     3      4    5
4 21.50 26.925 28.2
6 19.75 19.750 19.7
8 15.05     NA 15.4

@matloff
Copy link
Owner

matloff commented Jul 15, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants