获取R中唯一项的累积计数
我有以下表格: 日期 国家代码 2020-03-12 GBR 2020-03-16 胆碱酯酶 2020-03-16 胆碱酯酶 2020-03-18 SGP获取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
使用
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))