how to create categories conditionally using other variables values and sequence

╄→尐↘猪︶ㄣ 提交于 2019-12-06 22:24:59

First convert your variable to a character vector for proper coercion, and then use data.table

sampleDT$variable = as.character(sampleDT$variable)

sampleDT[, variable := paste(variable,1:.N,sep = ""), by = c("A", "B", "ID")]

This creates unique tallies based on the observed combinations of A, B, and ID.

This gets the following output:

    A  B ID variable     value
1: a1 b2  3       E1 0.6211421
2: a2 b2  4       E1 0.7421095
3: a1 b2  3       E2 0.3943915
4: a1 b1  1       E1 0.4069439
5: a2 b2  4       E2 0.7796073
6: a1 b2  3       E3 0.5505793
7: a1 b1  1       E2 0.3526222
8: a2 b2  4       E3 0.6906605
9: a1 b1  1       E3 0.2337894

which you can reorder if necessary.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!