错误:列comp.count的长度必须为1

我正在尝试将R代码转换成Matlab,用于Fama和法国因素组合的构建。我收到以下错误,我不知道该怎么办:

> # load("180619 data.ccm.RData")
> data.comp <- data.ccm %>%
+     rename(PERMNO=permno) %>% data.table %>% # ensure col names match crsp's
+     group_by(PERMNO) %>% 
+     mutate(datadate = as.yearmon(datadate),
+            comp.count = row(.)) %>% # number of years in data; future option to cut first year
+     # tests based on BE spread show FF no longer impose this condition
+     ungroup %>% arrange(datadate, PERMNO) %>% data.frame %>%
+     distinct(datadate, PERMNO, .keep_all = TRUE) 

Error: Column comp.count must be length 1 (the group size), not 12358968 In addition: There were 50 or more warnings (use warnings() to see the first 50)

Full code: https://drive.google.com/file/d/0BxvBvE2V-dFTVnZuLUFhZWNuazA/view

最好

评论
  • 奥巴喵
    奥巴喵 回复

    If you just want to count the number of rows by group, use n()

     mutate(datadate = as.yearmon(datadate),
            comp.count = n())