按条件按组进行排名或数字密集,忽略不满足条件的值

时间:2019-01-28 15:01:02

标签: r dense-rank

我下面有数据集示例:

name   date           value1 

grape  01-01-2018     127

grape  01-02-2018     124

grape  01-03-2018     154

grape  01-04-2018        

grape  01-05-2018     123

pear   01-01-2018     324

pear   01-02-2018     374

pear   01-03-2018        

pear   01-04-2018     312

所需的输出:

name   date          value1 producing days

grape  01-01-2018     127      1

grape  01-02-2018     124      2

grape  01-03-2018     154      3

grape  01-04-2018               

grape  01-05-2018     123      4

pear   01-01-2018     324      1

pear   01-02-2018     374      2

pear   01-03-2018               

pear   01-04-2018     312      3

我尝试过:

df2 <- df2 %>% 
  group_by(NAME) %>% 
  mutate(Producing_Days = dense_rank(Date))

我尝试过

df2 <- df2 %>% 
  group_by(NAME) %>% 
  mutate(Producing_Days = dense_rank(Date))

然后继续前进,但我无法让他们跳过没有价值的日子

1 个答案:

答案 0 :(得分:0)

尝试一下:您可以将is.na替换为value1==""

在@ demarsylvain提出建议后的最终答案

 df %>% group_by(name) %>% 
 mutate(Producing_days = ifelse(is.na(value1), NA,as.character(Date)) %>% 
          dense_rank())