遍历行并计算与R中的多个条件匹配的行数

时间:2019-05-09 18:10:57

标签: r loops dplyr multiple-conditions

我有一个看起来像这样的数据集:

        city period_day       date 
1  barcelona    morning 2017-01-15         
2  sao_paulo  afternoon 2016-12-07         
3  sao_paulo    morning 2016-11-16         
4  barcelona    morning 2016-11-06         
5  barcelona  afternoon 2016-12-31         
6  sao_paulo  afternoon 2016-11-30         
7  barcelona    morning 2016-10-15         
8  barcelona  afternoon 2016-11-30         
9  sao_paulo  afternoon 2016-12-24         
10 sao_paulo  afternoon 2017-02-02         

对于城市和period_day,我想计算每行的日期早于该行日期的行数。在这种情况下,我想要这样的结果:

        city period_day       date row_count
1  barcelona    morning 2017-01-15         2
2  sao_paulo  afternoon 2016-12-07         1
3  sao_paulo    morning 2016-11-16         0
4  barcelona    morning 2016-11-06         1
5  barcelona  afternoon 2016-12-31         1
6  sao_paulo  afternoon 2016-11-30         0
7  barcelona    morning 2016-10-15         0
8  barcelona  afternoon 2016-11-30         0
9  sao_paulo  afternoon 2016-12-24         2
10 sao_paulo  afternoon 2017-02-02         3

当row_count等于0时,表示它是较早的日期。

我想出了一个解决方案,但是花了太多时间来处理更多数据。那是代码:

get_count_function <- function(df) {
  idx <- 1:nrow(df)

  count <- sapply(idx, function(x) {
    name_city <-
      df %>% select(city) %>% filter(row_number() == x) %>% pull()
    name_period <-
      df %>% select(period_day) %>% filter(row_number() == x) %>% pull()

    date_row <- df %>%
      select(date) %>%
      filter(row_number() == x) %>%
      pull()

    date_any_row <- df %>%
      filter(dplyr::row_number() != x,
             city == name_city,
             period_day == name_period) %>%
      select(date) %>%
      pull()

    how_many <- sum(date_row > date_any_row)

    return(how_many)

  })

  return(count)

}

如何提高此功能的效率?

2 个答案:

答案 0 :(得分:1)

尝试这个:

library(tidyverse)

dat %>%
  group_by(city, period_day) %>%
  mutate(row_count = order(date) - 1) %>%
  ungroup()

调用order时,它将返回索引,指向选定值组(date)中值的顺序。从索引中减去1,就可以得出特定组中之前当前值的 count 。例如。如果是分钟。值在一个组中,它的索引为1,因此如果索引为1 - 1 = 0,则它的前面没有任何值(2)-只有一个值在它前面(一个旧的date之前)等。

数据:

dat <- read.table(
  text = "        city period_day       date
  barcelona    morning 2017-01-15
  sao_paulo  afternoon 2016-12-07
  sao_paulo    morning 2016-11-16
  barcelona    morning 2016-11-06
  barcelona  afternoon 2016-12-31
  sao_paulo  afternoon 2016-11-30
  barcelona    morning 2016-10-15
  barcelona  afternoon 2016-11-30
  sao_paulo  afternoon 2016-12-24
  sao_paulo  afternoon 2017-02-02",
  header = T,
  colClasses = c("character", "character", "Date")
)

答案 1 :(得分:1)

如果您愿意使用data.table软件包,这应该可以工作:

library(data.table)

dat <- read.table(header=T, row.names=1, text="
        city period_day       date 
1  barcelona    morning 2017-01-15         
2  sao_paulo  afternoon 2016-12-07         
3  sao_paulo    morning 2016-11-16         
4  barcelona    morning 2016-11-06         
5  barcelona  afternoon 2016-12-31         
6  sao_paulo  afternoon 2016-11-30         
7  barcelona    morning 2016-10-15         
8  barcelona  afternoon 2016-11-30         
9  sao_paulo  afternoon 2016-12-24         
10 sao_paulo  afternoon 2017-02-02   
")

dat <- as.data.table(dat)

dat[, row_count := (order(as.Date(date)) - 1), by=.(city, period_day)]

# Check
dat

##          city period_day       date row_count
##  1: barcelona    morning 2017-01-15         2
##  2: sao_paulo  afternoon 2016-12-07         1
##  3: sao_paulo    morning 2016-11-16         0
##  4: barcelona    morning 2016-11-06         1
##  5: barcelona  afternoon 2016-12-31         1
##  6: sao_paulo  afternoon 2016-11-30         0
##  7: barcelona    morning 2016-10-15         0
##  8: barcelona  afternoon 2016-11-30         0
##  9: sao_paulo  afternoon 2016-12-24         2
## 10: sao_paulo  afternoon 2017-02-02         3