Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr::mutate(cur\u group\u id)无法识别分组_R_Dplyr_Group By_Grouping - Fatal编程技术网

dplyr::mutate(cur\u group\u id)无法识别分组

dplyr::mutate(cur\u group\u id)无法识别分组,r,dplyr,group-by,grouping,R,Dplyr,Group By,Grouping,这是我的数据集 record_id voucher_number ice sets <int> <chr> <chr> <chr> 1 1 app1 app app 2 1 00000 1 1 3 1 11111 1 2

这是我的数据集

  record_id voucher_number ice   sets      
       <int> <chr>          <chr> <chr>
 1         1 app1           app   app                     
 2         1 00000          1     1    
 3         1 11111          1     2    
 4         1 22222          1     3    
 5         1 11111          2     4    
 6         2 app2           app   app  
 7         2 33333          1     1    
 8         2 44444          1     2    
 9         2 33333          2     3    
10         2 33333          3     4    
11         3 app3           app   app  
12         3 55555          1     1    
13         3 66666          1     2    
14         3 55555          2     3    
15         3 66666          2     4    
16         3 55555          3     5    
17         3 77777          1     6 
现在
cur\u group\u id
工作正常,但在记录id级别无法识别分组,结果如下:

  record_id voucher_number ice   voucher_sets   expectation_first  expect_last
       <int> <chr>          <chr> <chr>
 1         1 app1           app   1                                  application
 2         1 00000          1     2                    1                 1
 3         1 11111          1     3                    2                 2   
 4         1 22222          1     4                    3                 3   
 5         1 11111          2     3                    2                 2  
 6         2 app2           app   5                                  application
 7         2 33333          1     6                    1                 1
 8         2 44444          1     7                    2                 2   
 9         2 33333          2     6                    1                 1    
10         2 33333          3     6                    1                 1   
11         3 app3           app   8                                   application
12         3 55555          1     9                    1                 1
13         3 66666          1     10                   2                 2
14         3 55555          2     9                    1                 1  
15         3 66666          2     10                   2                 2   
16         3 55555          3     5                    1                 1   
17         3 77777          1     6                    3                 3
记录\u id凭证\u编号ice凭证\u设置期望值\u第一期望值\u最后期望值
1应用程序1应用程序1应用程序1应用程序
2         1 00000          1     2                    1                 1
3         1 11111          1     3                    2                 2   
4         1 22222          1     4                    3                 3   
5         1 11111          2     3                    2                 2  
6 2应用程序2应用程序5应用程序
7         2 33333          1     6                    1                 1
8         2 44444          1     7                    2                 2   
9         2 33333          2     6                    1                 1    
10         2 33333          3     6                    1                 1   
11 3应用程序3应用程序8应用程序
12         3 55555          1     9                    1                 1
13         3 66666          1     10                   2                 2
14         3 55555          2     9                    1                 1  
15         3 66666          2     10                   2                 2   
16         3 55555          3     5                    1                 1   
17         3 77777          1     6                    3                 3

我该如何处理这个问题以及出现了什么问题?

您的预期输出是什么?请参阅上面的
expectation\u first
expection\u last
列。这些应该是我预期的结果。但“申请”从何而来?在你的数据中没有任何东西表明你需要编写某种规则。我不知道你所说的“第一个&最后一个”是什么意思;组id来自所有分组变量的唯一组合,因此(记录\u id=1,凭证=000)将有一个组id,而(记录\u id=2,凭证=000)将有另一个id@camile,对不起,应该提到的是,
ice
列下的
app
的重新格式化值。当然这是我想要的,但是我想要每个记录id中的索引值,因为对于每个记录id,我有许多凭证编号。感谢您可以使用条件和lag()查看上面的行,并在发现“app”时重置计数
  record_id voucher_number ice   voucher_sets   expectation_first  expect_last
       <int> <chr>          <chr> <chr>
 1         1 app1           app   1                                  application
 2         1 00000          1     2                    1                 1
 3         1 11111          1     3                    2                 2   
 4         1 22222          1     4                    3                 3   
 5         1 11111          2     3                    2                 2  
 6         2 app2           app   5                                  application
 7         2 33333          1     6                    1                 1
 8         2 44444          1     7                    2                 2   
 9         2 33333          2     6                    1                 1    
10         2 33333          3     6                    1                 1   
11         3 app3           app   8                                   application
12         3 55555          1     9                    1                 1
13         3 66666          1     10                   2                 2
14         3 55555          2     9                    1                 1  
15         3 66666          2     10                   2                 2   
16         3 55555          3     5                    1                 1   
17         3 77777          1     6                    3                 3