简体   繁体   中英

Converting a base R loop into R data.table

I have a dataframe with records for every 15min values.

I'm using a loop to compute the number of records in a 15 min range. But its very slow. I feel converting it into a data.table functionality will speed up the process considerably.

i have written the following loop:

Input$Freq  <-  array()

for(i in 1:nrow(Input))
{
  Input[i,"Freq"]<-0
}

for(i in 1:nrow(Input))
{
  for(j in 1:nrow(Input))
  {
    if(Input[i,"Cur_DateTime"]  > Input[j,"Cur_DateTime"] & Input[i,"Cur_DateTime"] < Input[j,"Window_15"])
      Input[i,"Freq"] <- Input[i,"Freq"]+1
  }
}

I am not sure if this exactly answers your question. One can bin the data directly using base R's cut command. Here is an example:

#create a dummy sequence
s<-seq(from =as.POSIXct("2015-02-15 09:46:43.17"), by="5 min", length.out = 50)
df<-data.frame(s)

#bin the sequence by 15 minute intervals:
df$bins<-cut(df$s, "15 min")

Use the dplyr library to summarize:

library(dplyr)
summarise(group_by(df,bins), n())

or as Frank suggested:

count(df, bins)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM