Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
获取R中唯一项的累积计数_R - Fatal编程技术网

获取R中唯一项的累积计数

获取R中唯一项的累积计数,r,R,我有以下表格: 日期 国家代码 2020-03-12 GBR 2020-03-16 胆碱酯酶 2020-03-16 胆碱酯酶 2020-03-18 SGP 使用Reduce+stack+table transform( df, counts = sapply( Reduce(c,countrycode,accumulate = TRUE), function(x) paste0( do.call( paste, c(rev(st

我有以下表格:

日期 国家代码 2020-03-12 GBR 2020-03-16 胆碱酯酶 2020-03-16 胆碱酯酶 2020-03-18 SGP
使用
Reduce
+
stack
+
table

transform(
  df,
  counts = sapply(
    Reduce(c,countrycode,accumulate = TRUE),
    function(x) paste0(
      do.call(
        paste,
        c(rev(stack(table(x))),sep = ",")),
      collapse = ";"))
)
给予

数据

> dput(df)
structure(list(date = c("2020-03-12", "2020-03-16", "2020-03-16", 
"2020-03-18"), countrycode = c("GBR", "CHE", "CHE", "SGP")), class = "data.frame", row.names = c(NA, 
-4L))

tidyverse
df1%>%中的类似选项突变(tmp=accumulate(countrycode,c),tmp1=accumulate(rowid(countrycode),c),count=map2_chr(tmp,tmp1,~{i1有效。非常感谢!@wz1416如果答案有助于您通过单击左侧的复选标记来自由接受答案。每篇文章您只能接受一个答案。请参阅-@akrun,如果您可以用简单的术语解释语法将非常有帮助,因为它看起来非常复杂。最好将其作为answer@AnilGoyal这几乎类似于当我从他的帖子中得到这个想法时,托马斯开始嘲笑我
> dput(df)
structure(list(date = c("2020-03-12", "2020-03-16", "2020-03-16", 
"2020-03-18"), countrycode = c("GBR", "CHE", "CHE", "SGP")), class = "data.frame", row.names = c(NA, 
-4L))