简体   繁体   中英

Using 'window' functions in dplyr

I need to process rows of a data-frame in order, but need to look-back for certain rows. Here is an approximate example:

library(dplyr)
d <- data_frame(trial = rep(c("A","a","b","B","x","y"),2))
d <- d %>%
  mutate(cond = rep('', n()), num = as.integer(rep(0,n())))

for (i in 1:nrow(d)){
  if(d$trial[i] == "A"){
  d$num[i] <- 0
  d$cond[i] <- "A"
  }
else if(d$trial[i] == "B"){
  d$num[i] <- 0
  d$cond[i] <- "B"
  }
else{
  d$num[i] <- d$num[i-1] +1
  d$cond[i] <- d$cond[i-1]
  }
}

The resulting data-frame looks like

> d
Source: local data frame [12 x 3]

    trial cond num
 1      A    A   0
 2      a    A   1
 3      b    A   2
 4      B    B   0
 5      x    B   1
 6      y    B   2
 7      A    A   0
 8      a    A   1
 9      b    A   2
10      B    B   0
11      x    B   1
12      y    B   2

What is the proper way of doing this using dplyr ?

dlpyr -only solution:

d %>% 
  group_by(i=cumsum(trial %in% c('A','B'))) %>% 
  mutate(cond=trial[1],num=seq(n())-1) %>% 
  ungroup() %>% 
  select(-i)

#    trial cond num
# 1      A    A   0
# 2      a    A   1
# 3      b    A   2
# 4      B    B   0
# 5      x    B   1
# 6      y    B   2
# 7      A    A   0
# 8      a    A   1
# 9      b    A   2
# 10     B    B   0
# 11     x    B   1
# 12     y    B   2

Try

d %>% 
  mutate(cond = zoo::na.locf(ifelse(trial=="A"|trial=="B", trial, NA))) %>%
  group_by(id=rep(1:length(rle(cond)$values), rle(cond)$lengths)) %>% 
  mutate(num = 0:(n()-1))  %>% ungroup %>% 
  select(-id)

Here is one way. The first thing was to add A or B in cond using ifelse . Then, I employed na.locf() from the zoo package in order to fill NA with A or B. I wanted to assign a temporary group ID before I took care of num . I borrowed rleid() in the data.table package. Grouping the data with the temporary group ID (ie, foo ), I used row_number() which is one of the window functions in the dplyr package. Note that I tried to remove foo doing select(-foo) . But, the column wanted to stay. I think this is probably something to do with compatibility of the function.

library(zoo)
library(dplyr)
library(data.table)

d <- data_frame(trial = rep(c("A","a","b","B","x","y"),2))

mutate(d, cond = ifelse(trial == "A" | trial == "B", trial, NA),
          cond = na.locf(cond),
          foo = rleid(cond)) %>%
group_by(foo) %>%
mutate(num = row_number() - 1)

#   trial cond foo num
#1      A    A   1   0
#2      a    A   1   1
#3      b    A   1   2
#4      B    B   2   0
#5      x    B   2   1
#6      y    B   2   2
#7      A    A   3   0
#8      a    A   3   1
#9      b    A   3   2
#10     B    B   4   0
#11     x    B   4   1
#12     y    B   4   2

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