找到唯一的变量组合并创建一个新的变量,如果找到一个独特的组合

时间:2017-10-04 13:08:58

标签: r dplyr

我将此数据框作为玩具示例

aski = data.frame(A = c("x","y","z","x","z","z"),
                  B = c("a","b","c","a","b","c"))

现在我想检查A和B的每个独特组合,如果它是一个独特的组合我想在数据帧中创建一个新变量并每次递增(例如r1,r2,....)找到一个独特的组合

输出类似这样的数据框

aski2 = data.frame(A = c("x","y","z","x","z","z"),
                   B = c("a","b","c","a","b","c"),
                   output = c("r1","r2","r3","r1","r4","r3"))

3 个答案:

答案 0 :(得分:3)

试试这个:

aski2 <- data.frame(A = c("x","y","z","x","z","z"),
                    B = c("a","b","c","a","b","c"))
ref <- do.call(paste, aski2)
aski2$output <- paste("r", as.numeric(factor(ref, levels = unique(ref))),
                      sep = "")
aski2

答案 1 :(得分:1)

另一种选择是使用group_indices;按列 A B 分组,为每个组生成唯一ID (请参阅? group_indices ):

aski2 <- data.frame(A = c("x","y","z","x","z","z"), 
                    B = c("a","b","c","a","b","c"), 
                    C = c("s","v","g","v","g","d"))

aski2 %>% mutate(output = sprintf("r%s", group_indices(., A, B)))

#  A B C output
#1 x a s     r1
#2 y b v     r2
#3 z c g     r4
#4 x a v     r1
#5 z b g     r3
#6 z c d     r4

答案 2 :(得分:0)

一个选项是.GRP

library(data.table)
setDT(aski2)[, output := paste0("r", .GRP), .(A, B)]