gpt4 book ai didi

重命名 R 中的重复记录

转载 作者:行者123 更新时间:2023-12-04 10:33:02 27 4
gpt4 key购买 nike

我想根据出现的顺序重命名“产品”列 (data$Product) 中的重复类别。以下是数据框:

data <- data.frame(Product=c("1123Tr","5467Yr","15-f020WM","15-f020WM","15-f020WM","15-k153cl","17-f222nr","17-f222nr"),
Platform=c("caribian","flare","tease","brill","kittl","moui","mouner","fourt"),
Value = c(200,500,550,456,678,765,34,33))

下面是表格 - data.frame“数据”的样子:

   Product    Platform    Value
1123Tr caribian 200
5467Yr flare 500
15-f020WM tease 550
15-f020WM brill 456
15-f020WM kittl 678
15-k153cl moui 765
17-f222nr mouner 34
17-f222nr fourt 33

下面是所需的输出:

    Product        Platform    Value
1123Tr caribian 200
5467Yr flare 500
15-f020WM(D1) tease 550
15-f020WM(D2) brill 456
15-f020WM(D3) kittl 678
15-k153cl moui 765
17-f222nr(D1) mouner 34
17-f222nr(D2) fourt 33

有人可以给我一些提示或建议吗?

最佳答案

这与akrun的make.unique非常接近。

ave 会做这样的事情:

> ave(as.character(data$Product), data$Product, FUN=function(x) if (length(x)>1) paste0(x[1], '(', seq_along(x), ')') else x[1])
[1] "1123Tr" "5467Yr" "15-f020WM(1)" "15-f020WM(2)" "15-f020WM(3)" "15-k153cl" "17-f222nr(1)" "17-f222nr(2)"

关于重命名 R 中的重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com