如何在数据框的某些行上创建加权平均值

How to create a whighted average on some of the data frame's rows

我想创建一个查询,根据前 5 只鸟的数量计算 wofls 的平均数 rows.Is 有没有办法使用 sqldf 将计算限制在前 5 行?

这是我的玩具数据集和代码行:

df <- read.table(text = "dateTime         birds    wolfs     snakes
                            2014-05-21        9         7    a
                            2014-04-28        8         4    b
                            2014-04-13        2         8    c
                            2014-03-12        2         3    a
                            2014-02-04        8         3    a
                            2014-02-29        1         2    a
                            2014-01-17        7         1    b
                            2014-01-16        1         5    c
                            2014-09-20        9         7    c
                            2014-08-21        8         7    c ",header = TRUE)

library(sqldf)
g<-sqldf("select avg(wolfs*birds) from df ");g

你可以试试

library(sqldf)
sqldf("select avg(wolfs*birds) as weightavg
        from df
        where rowid <=5 ")
#   weightavg
#1      28.2

library(dplyr)
df %>% 
    slice(1:5) %>% 
    summarise(weightavg=mean(birds*wolfs))
#  weightavg
#1      28.2

或者

library(data.table)
setDT(df)[seq_len(.N)<=5, list(weightavg=mean(wolfs*birds))]
#   weightavg
#1:      28.2