如何在某些数据框的行上创建一个单独的平均值

时间:2015-07-28 06:59:21

标签: r dataframe sqldf

我想创建一个查询,根据前5行的鸟数计算wofls的平均值。是否有办法使用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

1 个答案:

答案 0 :(得分:1)

您可以尝试

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