【发布时间】: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
【问题讨论】: