【问题标题】:dplyr to check each firm has June historydplyr 检查每家公司的六月历史
【发布时间】:2015-04-08 06:16:17
【问题描述】:

我有一个 1m+ 的数据集,需要检查每个公司 (cusip) 和年份(fyear) 是否有 6 月份的观察结果,其中 datadate 是 YYYYMMDD。我尝试使用substr() 提取月份并进行逻辑测试,如果为真,则不要理会,但如果不是,cusip 将被删除。但是,这不起作用,并且返回关于非逻辑参数和条件长度的错误。我已经在dplyr 之外逐一检查,以确保一切正常,除了dplyr 内部之外,我没有遇到任何问题。任何帮助将不胜感激。

可重现的代码:

tdata <- structure(list(cusip = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 2), fyear = c(1962L, 
1963L, 1964L, 1965L, 1966L, 1967L, 1968L, 1969L, 1970L, 1971L, 
1972L, 1973L, 1974L, 1975L, 1976L, 1977L, 1978L, 1979L, 1980L, 
1981L, 1982L, 1983L, 1984L, 1985L, 1962L, 1963L, 1964L, 1965L, 
1966L, 1967L, 1969L), datadate = c(19620631L, 19630631L, 19640631L, 
19651231L, 19661231L, 19670631L, 19680631L, 19691231L, 19700631L, 
19710631L, 19720631L, 19730631L, 19740631L, 19751231L, 19760631L, 
19770631L, 19780631L, 19791231L, 19800631L, 19810631L, 19820631L, 
19831231L, 19841231L, 19850631L, 19621231L, 19630631L, 19640631L, 
19650631L, 19660631L, 19670631L, 19690631L)), .Names = c("cusip", "fyear", 
"datadate"), row.names = c(NA, 31L), class = "data.frame")

tdata %>% 
  group_by(cusip) %>% 
  group_by(fyear) %>% 
  arrange(desc(datadate)) %>% 
  if(substr(datadate[1], 5,6) != 06) cusip <- NULL

错误:

Error in if (.) as.numeric(substr(datadate[1], 5, 6)) != 6 else cusip <- NULL : 
  argument is not interpretable as logical
In addition: Warning message:
In if (.) as.numeric(substr(datadate[1], 5, 6)) != 6 else cusip <- NULL :
  the condition has length > 1 and only the first element will be used

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    为什么不先为月份创建一个列?比如:

    library(dplyr)
    tdata$month <- substr(tdata$datadate, 5, 6)
    
    tdata %>%
      group_by(cusip, fyear) %>%
      mutate(has_June = month == "06")
    

    请注意,月份是一个字符串,因此要检查是否相等,您需要使用引号。

    一气呵成:

    tdata %>%
      group_by(cusip, fyear) %>%
      mutate(month = substr(datadate, 5, 6),
             has_June = month == "06")
    

    然后你可以找到那些没有六月的加上:%&gt;% filter(month != "06")

    【讨论】:

    • 实际上,这是应用一个逻辑,如果它有六月作为月份,则为真,否则为假。我需要它来检查每年是否有 6 月的数据集,如果有,请保留该年的所有月份。否则,放弃那一年的那个 cusip 代码。
    • 然后改成has_June = any(month == "06")
    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多