[英]R, sqldf and avg for ratio average
Why do the two commands yield different results when using sqldf
in R? 为什么在R中使用
sqldf
时这两个命令会产生不同的结果?
sqldf('select species,
avg([Petal.Width]/[Petal.Length])
as petalratio from iris group by species')
sqldf('select species,
([Petal.Width]/[Petal.Length])
as petalratio from iris group by species')
Being that the goal is to find the average of the ratios for each of the 3 species. 这样做的目的是找到3个物种中每个物种的平均比率。
select species,
avg([Petal.Width]/[Petal.Length]) as petalratio
from iris
group by species
This query outputs the average per species as you are using an aggregate function avg
. 使用聚合函数
avg
此查询输出每个物种的avg
。
select species,
([Petal.Width]/[Petal.Length]) as petalratio
from iris
group by species
This query randomly outputs one row per species as you aren't using an aggregate function. 由于您没有使用聚合函数,因此该查询为每个物种随机输出一行。 This isn't allowed in most databases, but allowed in SQLite, which is the default database
sqldf
uses. 在大多数数据库中不允许这样做,但在SQLite(
sqldf
使用的默认数据库)中是sqldf
。
You should use the first query with avg
as that is what you are trying to do. 您应该将第一个查询与
avg
一起使用,因为这是您要尝试的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.