简体   繁体   中英

apply a very particular function to a each level of a factor in a dataframe in R

I would like to understand what I am doing wrong when applying this particular function to my dataframe.

The libraries requested are

library(dplyr)
library(magrittr)
library(lubridate)

First of all, the dataframe

t1<-c("2009_01_01 09:00:00","2009_01_01 09:01:00","2009_01_01 09:02:00","2009_01_01 09:03:00","2009_01_01 09:04:00","2009_01_01 09:05:00")
t2<-c("2009_01_01 09:00:00","2009_01_01 09:01:00","2009_01_01 09:02:00")
S1<-cbind("A",t1)
S2<-cbind("B",t2)
datat<-data.frame(rbind(S1,S2))
datat$N<-"NA"
colnames(datat)<-c("trap","time","N")
datat$time<-as.POSIXct(datat$time, "%Y_%m_%d %H:%M:%S",tz="")
listdataset<-split(datat,datat$trap)
trap<-names(listdataset)
listdataset <- lapply(seq_along(listdataset), function(x) as.data.frame(listdataset[[x]]))
names(listdataset)<-trap
list2env(listdataset, envir = .GlobalEnv)

Then the particular function (please note it has been developed by a member of this community, sadly I don't remember his/her name and so I apologize for not providing the right credit of his/her work) which was developed for a single dataframe. It has been "slightly" modified by me (the i in the original version was the name of the dataset) for this specific purpose. event_count <- function(ref_time){ min(i %>% filter(time %within% interval(ref_time - 60*5, ref_time)) %>% nrow, 10) }

My expected out come is

datat$N<-c(1,2,3,4,5,6,1,2,3)
datat
 trap        time         N
 1    A 2009-01-01 09:00:00 1
 2    A 2009-01-01 09:01:00 2
 3    A 2009-01-01 09:02:00 3
 4    A 2009-01-01 09:03:00 4
 5    A 2009-01-01 09:04:00 5
 6    A 2009-01-01 09:05:00 6
 7    B 2009-01-01 09:00:00 1
 8    B 2009-01-01 09:01:00 2
 9    B 2009-01-01 09:02:00 3

What I have been able to do so far is not producing the expected outcome.

for (i in listdataset) { 
i %<>%   
rowwise() %>% 
mutate(N = event_count(time)) %>% 
arrange(time)
}

i
A tibble: 3 x 3
trap      time                  N
<fct>    <dttm>              <dbl>
1 B     2009-01-01 09:00:00     1
2 B     2009-01-01 09:01:00     2
3 B     2009-01-01 09:02:00     3

What I am doing wrong? Any advice would be very appreciated! What I have understood from my output is that only the second element of the loop is evaluated. How to get evaluated also the first element?

This solves your problem:

event_count <- function(i,ref_time){
  min(i %>% filter(time %within% interval(ref_time - 60*5, ref_time)) %>% nrow, 10)
}

newData <- bind_rows(lapply(listdataset,function(i){i %>%   
    rowwise() %>% 
    mutate(N = event_count(i,time)) %>% 
    arrange(time)}))

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