mql4beginner
mql4beginner

Reputation: 2233

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

I would like to create a query that calculate a whighted average of wofls based on number of birds for the first 5 rows.Is there a way to use sqldf to limit the calculation to the first 5 rows?

Here is my toy dataset and line of code:

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

Upvotes: 1

Views: 67

Answers (1)

akrun
akrun

Reputation: 887213

You may try

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

Or

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

Or

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

Upvotes: 1

Related Questions