Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 循环遍历一个表以获取另一个表中的计数_R - Fatal编程技术网

R 循环遍历一个表以获取另一个表中的计数

R 循环遍历一个表以获取另一个表中的计数,r,R,在R中,我有一个种子表,如下所示: 种子盘 |========|================| | date | classification | |========|================| | 201501 | A | | 201501 | A | | 201501 | A | | 201502 | B | | 201502 | B | | 20

在R中,我有一个种子表,如下所示:

种子盘

|========|================|
| date   | classification |
|========|================|
| 201501 | A              |
| 201501 | A              |
| 201501 | A              |
| 201502 | B              |
| 201502 | B              |
| 201502 | B              |
| ...    | ...            |
数据表如下所示

数据:

我正在编写下面的代码,以获取每个月的“活动观察”数量以及种子表中的分类。活动观察是一种观察,其在种子表中行的
创建日期=月份

n <- nrow(seed_table)
num_obs <- numeric(n)
for (row in 1:n) {
    num_obs[row] <- (sum(
        data$Created_Date >= seed_table[row, "date"] &
            data$End_Date <= seed_table[row, "date"] &
            data$classification == seed_table[row, "classification"]))
    cat(n - row)
}  

n正如@eric fail所建议的,您应该使用
dput()
来共享您的数据。例如:

seed_table <- structure(list(
  date = c(201501L, 201501L, 201502L), 
  classification = structure(
    c(1L, 1L, 2L), .Label = c("A", "B"), class = "factor")), 
  .Names = c("date", "classification"), 
  row.names = c(1L, 2L, 4L), class = "data.frame")
data <- structure(list(
  ID = 1:6, 
  Create_Date = c(201501L, 201501L, 201502L, 201412L, 201412L, 201502L), 
  End_Date = c(201601L, 201605L, 201601L, 201501L, 201502L, 201503L), 
  classification = structure(c(1L, 2L, 2L, 1L, 2L, 1L), 
    .Label = c("A", "B"), class = "factor")), 
  .Names = c("ID", "Create_Date", "End_Date", "classification"), 
  class = "data.frame", row.names = c(NA, -6L))

请注意,您的代码中有一些错误。你提到的是
Created_Date
,而不是
Create_Date
,并且(我相信)你有自己的不平等(
=
请提交一个最小可复制的示例。请参阅:。具体来说,你应该使用
dput()
阅读和汇总:,在R中共享你的(最小可复制)数据
seed_table <- structure(list(
  date = c(201501L, 201501L, 201502L), 
  classification = structure(
    c(1L, 1L, 2L), .Label = c("A", "B"), class = "factor")), 
  .Names = c("date", "classification"), 
  row.names = c(1L, 2L, 4L), class = "data.frame")
data <- structure(list(
  ID = 1:6, 
  Create_Date = c(201501L, 201501L, 201502L, 201412L, 201412L, 201502L), 
  End_Date = c(201601L, 201605L, 201601L, 201501L, 201502L, 201503L), 
  classification = structure(c(1L, 2L, 2L, 1L, 2L, 1L), 
    .Label = c("A", "B"), class = "factor")), 
  .Names = c("ID", "Create_Date", "End_Date", "classification"), 
  class = "data.frame", row.names = c(NA, -6L))
m1 <- outer(seed_table$date, data$Create_Date, ">=")
m2 <- outer(seed_table$date, data$End_Date, "<=")
m3 <- outer(seed_table$classification, data$classification, "==")
m <- m1 & m2 & m3
num_obs <- apply(m, 1, sum)