简体   繁体   中英

Using dplyr to get cumulative count by group

Thanks in advance. I have the following data:

df <- data.frame(person=c(1,1,1,1,2,2,2,2,3,3,3,3), 
             neighborhood=c("A","A","A","A","B","B","C","C","D","D","E","F"))

I would like to generate a new column that gives the cumulative count of neighborhoods that each person moves through as the panel progresses. Like such:

df2 <- data.frame(person=c(1,1,1,1,2,2,2,2,3,3,3,3), 
             neighborhood=c("A","A","A","A","B","B","C","C","D","D","E","F"),
             moved=c(0,0,0,0,0,0,1,1,0,0,1,2)
             )

Thanks again.

We can use group by 'person', then create the 'moved' by match ing the 'neighborhood' with its unique values to get the index and subtract 1.

df %>%
   group_by(person) %>% 
   mutate(moved = match(neighborhood, unique(neighborhood))-1)
#   person neighborhood moved
#    <dbl>       <fctr> <dbl>
#1       1            A     0
#2       1            A     0
#3       1            A     0
#4       1            A     0
#5       2            B     0
#6       2            B     0
#7       2            C     1
#8       2            C     1
#9       3            D     0
#10      3            D     0
#11      3            E     1
#12      3            F     2

or use factor with levels specified as the unique values in 'neighborhood', coerce to 'integer' and subtract 1.

df %>%
   group_by(person) %>% 
   mutate(moved = as.integer(factor(neighborhood, levels = unique(neighborhood)))-1)
#   person neighborhood moved
#    <dbl>       <fctr> <dbl>
#1       1            A     0
#2       1            A     0
#3       1            A     0
#4       1            A     0
#5       2            B     0
#6       2            B     0
#7       2            C     1
#8       2            C     1
#9       3            D     0
#10      3            D     0
#11      3            E     1
#12      3            F     2

This can also easily be achieved with rleid or the frank functions from the data.table package:

library(data.table)
# with 'rleid'
setDT(df)[, moved := rleid(neighborhood)-1, by = person]
# with 'frank'
setDT(df)[, moved := frank(neighborhood, ties.method='dense')-1, by = person]

the result:

> df
    person neighborhood moved
 1:      1            A     0
 2:      1            A     0
 3:      1            A     0
 4:      1            A     0
 5:      2            B     0
 6:      2            B     0
 7:      2            C     1
 8:      2            C     1
 9:      3            D     0
10:      3            D     0
11:      3            E     1
12:      3            F     2

With dplyr you could use the dense_rank function:

library(dplyr)
df %>%
  group_by(person) %>%
  mutate(moved = dense_rank(neighborhood)-1)

This can be achieved using window functions of dplyr , as well. Here is the code:

library(dplyr)
my.df <- tbl_df(df)

my.df %>% 
    # Per person
    group_by(person) %>% 
    # sort by neighborhood
    arrange(neighborhood) %>%
    # if the neighborhood has changed compared to the row before
    mutate(moved = (neighborhood != lag(neighborhood))) %>%
    # turn NAs (first rows) into FALSE
    mutate(moved = ifelse(is.na(moved), FALSE, moved)) %>%
    # use cumulative sum of the logical column to get number of moves
    mutate(no_moves = cumsum(moved))

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