使用管道系统时,如何获得功能正常的功能? [英] How do I get mean functions to work when I use piping?
问题描述
这可能是一个简单的问题,但是我很难让dplyr的平均值函数起作用.
This is probably a simple question, but I'm having trouble getting the mean function to work using dplyr.
以mtcars数据集为例,如果我输入:
Using the mtcars dataset as an example, if I type:
data(mtcars)
mtcars %>%
select (mpg) %>%
mean()
我收到警告消息:在mean.default(.)中:参数不是数字或逻辑:返回"NA"错误消息.
I get the "Warning message: In mean.default(.) : argument is not numeric or logical: returning NA" error message.
由于某些原因,如果我重复相同的代码,而只是要求摘要",范围"或其他几种统计计算,则它们可以正常工作:
For some reason though if I repeat the same code but just ask for a "summary", or "range" or several other statistical calculations, they work fine:
data(mtcars)
mtcars %>%
select (mpg) %>%
summary()
类似地,如果我以基数R表示法运行mean函数,那也可以正常工作:
Similarly, if I run the mean function in base R notation, that works fine too:
mean(mtcars$mpg)
谁能指出我做错了什么?
Can anyone point out what I've done wrong?
推荐答案
在 dplyr
中,只要不更改原始数据帧,就可以使用 summarise()
(重新排序,过滤,添加等),而是创建了一个 new 数据框,该数据框具有第一个数据框的摘要统计信息.
In dplyr
, you can use summarise()
whenever you're not changing your original dataframe (reordering it, filtering it, adding to it, etc), but instead are creating a new dataframe that has summary statistics for the first dataframe.
mtcars %>%
summarise(mean_mpg = mean(mpg))
给出输出:
mean_mpg
1 20.09062
PS.如果您正在学习 dplyr
,则学习这五个动词将使您走很长一段路: select()
, filter()
,group_by()
, summarise()
, arrange()
.
PS. If you're learning dplyr
, learning these five verbs will take you a long way: select()
, filter()
, group_by()
, summarise()
, arrange()
.
这篇关于使用管道系统时,如何获得功能正常的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!