R coding: How to keep records with 4 complete quarters of data

前端 未结 3 572
囚心锁ツ
囚心锁ツ 2021-01-27 01:07

I have a dataframe with company quarterly data and have this question:

How can I retain records for only those companies with 4 quarters of data (as companies sometimes

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 01:27

    The following code can help you ....

    final=data.frame()
    for(i in unique(data$company)){
    temp=data[data$company==i,]
    for(j in unique(temp$year)){
        if(nrow(temp[temp$year==j,])==4)
            final=rbind(final,data.frame(company=i,Year=j))
    }
    }
    

    'final' dataframe will contain your required fields.

提交回复
热议问题