如何在apply循环中使计数器递增

时间:2018-04-22 21:12:33

标签: r apply

我正在尝试计算一个数据帧的每一行的计数器,第1列需要等于“vsrv11”,第3列必须是需要2017年的日期。 所以我做了这个代码,计数器在if语句中递增,但是对于循环的每次迭代,计数器再次变为0。

.whl

2 个答案:

答案 0 :(得分:2)

一般来说,我建议不要使用全局变量,也可以通过简单过滤来检查。

df <- data.frame(x = sample(c("vsrv11", rnorm(10)), 100, replace = TRUE),
                 y = rnorm(100),
                 z = as.character(sample(c(2017, 2018), 100, replace = TRUE)))


nrow(df[df[, 1] == "vsrv11" & grepl("2017", df[, 3]), ])

或只是

sum(df[, 1] == "vsrv11" & grepl("2017", df[, 3]))

答案 1 :(得分:0)

tidyverse中,您可以使用dplyr::count执行此类操作:

# Sample data
vpnsessions <- data.frame(
    srv = "vsrv11",
    id = c(rep("2017_abc", 10), rep("2018_def", 8)),
    stringsAsFactors = F)

library(dplyr);
count(vpnsessions, year = substr(id, 1, 4))
## A tibble: 2 x 2
#  year      n
#  <chr> <int>
#1 2017     10
#2 2018      8

请注意count如何计算id s的出现次数。从结果data.frame / tibble中提取相关行很容易。

要点评,在R索引中,1不是0而是substring(..., 0, 4),因此代码中的substring(..., 1, 4)应为id <- c("1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3") type <- c("A", "A", "B", "A", "B", "B", "B", "B", "A", "B", "B", "A", "A", "B", "A") date_collected <- as.Date(c("2018-01-01", "2018-01-01", "2018-01-01", "2018-01-02", "2018-01-02", "2018-01-02", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-04", "2018-01-04", "2018-01-04", "2018-01-05", "2018-01-05", "2018-01-05")) df <- data.frame(id, type, date_collected)