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